Forum Moderators: coopster
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";
?>
<?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...]
echo $xml->person[0]->firstname;
I removed one of the m's :)
<?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.
If I understand your question correctly, yes.
and Welcome to WebmasterWorld! :)
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]
$uri = $xml->RESPONSE->KEY[1]['_Value'];