help parsing SimpleXMLElement
Posted: Thu Jan 06, 2011 9:55 am
I'm getting back an XML which I'm looking at as a SimpleXMLElement:
it prints nicely to a page using the print_r($Data) command. But I'm confused about how to access the actual values. For example, part of the print_r output looks like this:
Can someone show me the correct syntax?
Thanks.
Code: Select all
$xmlFileData = file_get_contents($url);
$Data = new SimpleXMLElement($xmlFileData);
echo "First Name is $firstName";
echo "<br />";
echo "<pre>";
print_r($Data);
echo "</pre>";
I've tried various ways to access the actual values, without success. I'm confused by the multiples levels in the returned $Data objectSimpleXMLElement Object
(
[legislators] => SimpleXMLElement Object
(
[legislator] => Array
(
[0] => SimpleXMLElement Object
(
[website] => http://house.gov/velazquez/
[fax] => 202-226-0327
[govtrack_id] => 400416
[firstname] => Nydia
[chamber] => house
[middlename] => M.
Code: Select all
$firstName = $Data->[legislators][legislator][0][firstname];;
//$firstName = $Data[legislators][legislator][0][firstname];
//echo "First Name is $Data[legislators][legislator][0][firstname=>value]";
Thanks.