I want to parse XML coming from this URL: http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=1DN1Y0GF2HTSA76NRQG2&Operation=BrowseNodeLookup&ResponseGroup=BrowseNodeInfo&BrowseNodeId=1000 I want Children->BrowseNode->BrowseNodeId and Children->BrowseNode->Name from this XML. I have tried this code: $url = http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=1DN1Y0GF2HTSA76NRQG2&Operation=BrowseNodeLookup&ResponseGroup=BrowseNodeInfo&BrowseNodeId=1000 $xml = new SimpleXMLElement($url,null,true); foreach($xml as $node) { echo $node->BrowseNode->BrowseNodeId."-". $BrowseNode->BrowseNode->Name; } Code (markup): But I only get this output : 1000 - Subjects (And I do not get further output). Can someone please help me to solve this problem.
I think what you want is this: <?php $url = 'http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=1DN1Y0GF2HTSA76NRQG2&Operation=BrowseNodeLookup&ResponseGroup=BrowseNodeInfo&BrowseNodeId=1000'; $xml = new SimpleXMLElement($url,null,true); foreach ($xml->BrowseNodes->BrowseNode->Children->BrowseNode as $browseNode) { echo $browseNode->BrowseNodeId."-".$browseNode->Name."<br>\n"; } ?> PHP:
<?php $url = file_get_contents('http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=1DN1Y0GF2HTSA76NRQG2&Operation=BrowseNodeLookup&ResponseGroup=BrowseNodeInfo&BrowseNodeId=1000'); $xml = new SimpleXMLElement($url); foreach ($xml->BrowseNodes->BrowseNode->Children->BrowseNode as $browseNode) { echo $browseNode->BrowseNodeId."-".$browseNode->Name."<br>\n"; } ?> PHP: Output: 1-Arts & Photography 2-Biographies & Memoirs 3-Business & Investing 4-Children's Books 4366-Comics & Graphic Novels 5-Computers & Internet 6-Cooking, Food & Wine 86-Entertainment 301889-Gay & Lesbian 10-Health, Mind & Body 9-History 48-Home & Garden 10777-Law 17-Literature & Fiction 13996-Medicine 18-Mystery & Thrillers 53-Nonfiction 290060-Outdoors & Nature 20-Parenting & Families 173507-Professional & Technical 21-Reference 22-Religion & Spirituality 23-Romance 75-Science 25-Science Fiction & Fantasy 26-Sports 28-Teens 27-Travel
You're welcome. Of course good documentation for SimpleXMLElement is available at http://www.php.net/simplexml/. The complication in this case was that the XML repeatedly uses the same element name("BrowseNodeId") at different levels throughout the document, so you have to be explicit about which element it is that you want the value of.