Help parsing XML where tags have children (Amazon)

Discussion in 'PHP' started by adsensing, Jul 18, 2007.

  1. #1
    Hey,

    I've been reading tutorials to try and learn how to parse Amazon's ECS xml feed. I've been able to parse the ASIN and DetailPageUrl, but all the other tags in the XML feed are children of other tags like:

    <SmallImage>
    <URL>http://ec1.images-amazon.com/images/I/11ynduRZT1L.jpg</URL>
    <Height Units="pixels">75</Height>
    <Width Units="pixels">75</Width>
    </SmallImage>

    I want to retrieve the SmallImage->URL tag. Most of the syntax I've tried don't return anything unless I only use the tag "url", which returns all the image urls (small, medium, large, as in the example below).

    So basically, what should I change to bring in the SmallImage->Url?

    Here is the code I'm using:

    
    <?php
    
    $counter = 0;
    $type = 0;
    $tag = "";
    $itemInfo = array();
    $channelInfo = array();
    
    function opening_element($xmlParser, $name, $attribute){
    
    global $tag, $type;
    
    $tag = $name;
    
    if($name == "RequestProcessingTime"){
    $type = 1;
    }
    else if($name == "ITEM"){
    $type = 2;
    }
    
    }//end opening element
    
    
    function closing_element($xmlParser, $name){
    
    global $tag, $type, $counter;
    
    $tag = "";
    if($name == "ITEM"){
    $type = 0;
    $counter++;
    }
    else if($name == "ITEMSEARCHRESPONSE"){
    $type = 0;
    }
    }//end closing_element
    
    function c_data($xmlParser, $data){
    
    global $tag, $type, $itemInfo, $counter;
    
    $data = trim(htmlspecialchars($data));
    
    if($tag == "ASIN" || $tag == "DETAILPAGEURL" || $tag == "TITLE" || $tag == "URL"){
    if($type == 1){
    
    
    
    }//end checking channel
    else if($type == 2){
    
    $itemInfo[$counter][strtolower($tag)] .= $data;
    
    }//end checking for item
    }//end checking tag
    }//end cdata funct
    
    $xmlParser = xml_parser_create();
    
    xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, TRUE);
    xml_parser_set_option($xmlParser, XML_OPTION_SKIP_WHITE, TRUE);
    
    xml_set_element_handler($xmlParser, "opening_element", "closing_element");
    xml_set_character_data_handler($xmlParser, "c_data");
    
    $fp = file("http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&Version=2005-03-23&Operation=ItemSearch&ContentType=text%2Fxml&SubscriptionId=1WWR3NK01ZKDPXB5FE82&AssociateTag=cd-home-20&SearchIndex=PetSupplies&Keywords=dog%20clothes&ResponseGroup=Medium");
    
    foreach($fp as $line){
    if(!xml_parse($xmlParser, $line)){
    die("Could not parse file.");
    }
    }
    
    ?>
    <html>
    <head>
    <title>Test</title>
    </head>
    <body>
    <h1>Test</h1>
    Description of Feed: <br /><br />
    <?php
    foreach($itemInfo as $items){
    echo $items["title"]."<br />";
    echo $items["url"]."<br />";
    echo "<a href='".$items["detailpageurl"]."'>".$items["asin"]."</a><br /><br />";
    }
    ?>
    </body>
    </html>
    Code (markup):
    and here's the XML link: http://ecs.amazonaws.com/onca/xml?S...ies&Keywords=dog clothes&ResponseGroup=Medium

    Thanks.
     
    adsensing, Jul 18, 2007 IP
  2. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Sorry to bring this back, it seems to be old. But does anyone have some advice for this post? I am in the same situation now.
     
    glasglow, Mar 19, 2009 IP