1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

XML Parsing

Discussion in 'PHP' started by jbashir, Feb 14, 2008.

  1. #1
    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.
     
    jbashir, Feb 14, 2008 IP
  2. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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:
     
    stoli, Feb 14, 2008 IP
    lordadel likes this.
  3. KnuTz

    KnuTz Well-Known Member

    Messages:
    169
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #3
    
    <?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
     
    KnuTz, Feb 14, 2008 IP
    lordadel likes this.
  4. jbashir

    jbashir Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    stoli and KnuTz

    Thanks for the help.
     
    jbashir, Feb 14, 2008 IP
  5. KnuTz

    KnuTz Well-Known Member

    Messages:
    169
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #5
    no problem man
     
    KnuTz, Feb 14, 2008 IP
  6. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    stoli, Feb 14, 2008 IP