Forum Moderators: coopster

Message Too Old, No Replies

cannot parse xml with php

         

michaelh613

7:33 pm on Mar 20, 2008 (gmt 0)

10+ Year Member




Forum Newbie

Joined: Sun Mar 16, 2008 2:35 pm
Posts: 20 Essentially I have some basic code out of the PHP Cookbook that should parse a xml document and print the firstname in this case. However clearly I have made a mistake. I originally tried using this technique on a much more complex xml and founcd that didn't work so I went t simplify it. It just get no output


<?php
$ab='<?xml version="1.0">';
$ab.='<address-book>';
$ab.='<person id="1"';
$ab.='<firstname>David</firstname>';
$ab.='<lastname>Sklar</lastname>';
$ab.='</person>';
$ab.='</address-book>';

//echo $ab;
print $ab->person['firstname'] ."\n";
?>

phparion

7:58 pm on Mar 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it doesnt work like this friend, search XMLDOM or SIMPLEXML on php.net to see how to parse xml with php.

michaelh613

8:06 pm on Mar 20, 2008 (gmt 0)

10+ Year Member



This is straight from a book on the subject. I'm doing searches but not finding the problem. Could you give me a hint to the issue.

michaelh613

8:14 pm on Mar 20, 2008 (gmt 0)

10+ Year Member



New code doesn't work explicitly creating the xml object

<?php
$ab='<?xml version="1.0"?>';
$ab.='<address-book>';
$ab.='<person id="1">';
$ab.='<firstname>David</firstname>';
$ab.='<lastname>Sklar</lastname>';
$ab.='</person>';
$ab.='</address-book>';

//$bc = new SimpleXMLElement($xmlstr);
//$bc=simplexml_load_string($ab);
//echo $ab;
//$ab=simplexml_load_file('address-book.xml');
//echo $ab;

//echo $bc->person['firstname'] ."\n";

$xml = new SimpleXMLElement($ab);

echo $xml->person[0]->firstnamme;
?>
[/code]

Basically following the example on
[us3.php.net...]

eelixduppy

9:54 pm on Mar 20, 2008 (gmt 0)



There is a spelling error in firstname. It should be as follows:

echo $xml->person[0]->firstname;

I removed one of the m's :)

michaelh613

10:05 pm on Mar 20, 2008 (gmt 0)

10+ Year Member



Had a few issues. Actually this example is working now as

<?php
$ab='<?xml version="1.0"?>';
$ab.='<address-book>';
$ab.='<person id="1">';
$ab.='<firstname>David</firstname>';
$ab.='<lastname>Sklar</lastname>';
$ab.='</person>';
$ab.='</address-book>';

$bc=simplexml_load_string($ab);
echo $bc->person->firstname ."\n";
echo $bc->person['id'] ."\n";
?>

Question do you always have to start at the top element and iterate down person->lastname vs going to lastname when it is unique. It appears so.

eelixduppy

10:17 pm on Mar 20, 2008 (gmt 0)



>> start at the top element and iterate down person->lastname vs going to lastname when it is unique.

If I understand your question correctly, yes.

and Welcome to WebmasterWorld! :)

michaelh613

10:29 pm on Mar 20, 2008 (gmt 0)

10+ Year Member



Thanks that answers the real world question that got me started in the first place.

michaelh613

2:36 am on Mar 21, 2008 (gmt 0)

10+ Year Member



I am going to add one more question

I am getting this xml as a response

<RESPONSE_GROUP MISMOVersionID="2.1">
- <RESPONSE>
<KEY _Name="MemberID" _Value="100" />
<KEY _Name="ResponseURI"
_Value="https:\\example.net\ConsumerInte
rface\SomePage.asp?settings=sdfjas2143245123adfasdf
asdfasdf" />

I am using this response

$uri = $xml->RESPONSE->KEY['_Value'];

My problem is I am getting the first Key _Value. How do I code it to get the 2nd Key _Value

[edited by: eelixduppy at 3:32 am (utc) on Mar. 21, 2008]
[edit reason] exemplified [/edit]

eelixduppy

3:27 am on Mar 21, 2008 (gmt 0)



Try the following:

$uri = $xml->RESPONSE->KEY[1]['_Value'];

michaelh613

4:39 am on Mar 21, 2008 (gmt 0)

10+ Year Member



Yep that worked. Simple really. Just need to think of multiple values as an array. Makes sense now that I think of it.

Thanks

phranque

10:50 am on Mar 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], michaelh613!