Jump to content

[SOLVED] Simple XML parsing problems (coming back blank)


Recommended Posts

My xml file (example) is like this

<?xml version="1.0" encoding="UTF-8"?><ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05"><OperationRequest><HTTPHeaders><Header Name="UserAgent"></Header></HTTPHeaders><RequestId>1QPP350WQXB5R08VSM7B</RequestId><Arguments><Argument Name="SearchIndex" Value="DVD"></Argument><Argument Name="Service" Value="AWSECommerceService"></Argument><Argument Name="Operation" Value="ItemSearch"></Argument><Argument Name="Actor" Value="Brad Pitt"></Argument><Argument Name="AWSAccessKeyId" Value="06NSVBP9J0TT6AXYGER2"></Argument></Arguments><RequestProcessingTime>0.174333810806274</RequestProcessingTime></OperationRequest><Items><Request><IsValid>True</IsValid><ItemSearchRequest><Actor>Brad Pitt</Actor><SearchIndex>DVD</SearchIndex></ItemSearchRequest></Request><TotalResults>179</TotalResults><TotalPages>18</TotalPages><Item><ASIN>B000TGGJKU</ASIN><DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B000TGGJKU%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B000TGGJKU%253FSubscriptionId=06NSVBP9J0TT6AXYGER2</DetailPageURL><ItemAttributes><Actor>Brad Pitt</Actor><Actor>Eric Bana</Actor><Actor>Orlando Bloom</Actor><Director>Wolfgang Petersen</Director><Manufacturer>Warner Home Video</Manufacturer><ProductGroup>DVD</ProductGroup><Title>Troy (Director's Cut) [blu-ray]</Title></ItemAttributes></Item><Item><ASIN>B000TGJ8IU</ASIN><DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B000TGJ8IU%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B000TGJ8IU%253FSubscriptionId=06NSVBP9J0TT6AXYGER2</DetailPageURL><ItemAttributes><Actor>Brad Pitt</Actor><Actor>Eric Bana</Actor><Actor>Orlando Bloom</Actor><Manufacturer>Warner Home Video</Manufacturer><ProductGroup>DVD</ProductGroup><Title>Troy - Director's Cut (Ultimate Collector's Edition)</Title></ItemAttributes></Item><Item><ASIN>B00003W8NM</ASIN><DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B00003W8NM%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B00003W8NM%253FSubscriptionId=06NSVBP9J0TT6AXYGER2</DetailPageURL><ItemAttributes><Actor>Edward Norton</Actor><Actor>Brad Pitt</Actor><Actor>Meat Loaf</Actor><Actor>Helena Bonham Carter</Actor><Actor>Zach Grenier</Actor><Director>David Fincher</Director><Manufacturer>20th Century Fox</Manufacturer><ProductGroup>DVD</ProductGroup><Title>Fight Club (Two-Disc Collector's Edition)</Title></ItemAttributes></Item><Item><ASIN>B000062XHI</ASIN><DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B000062XHI%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B000062XHI%253FSubscriptionId=06NSVBP9J0TT6AXYGER2</DetailPageURL><ItemAttributes><Actor>Don Cheadle</Actor><Actor>George Clooney</Actor><Actor>Holly Marie Combs</Actor><Actor>Matt Damon</Actor><Actor>Michael de Lano</Actor><Creator Role="Cinematographer">Peter Andrews</Creator><Creator Role="Composer">David Holmes</Creator><Manufacturer>Warner Home Video</Manufacturer><ProductGroup>DVD</ProductGroup><Title>Ocean's Eleven (Widescreen Edition)</Title></ItemAttributes></Item><Item><ASIN>B000TGGJKK</ASIN><DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B000TGGJKK%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B000TGGJKK%253FSubscriptionId=06NSVBP9J0TT6AXYGER2</DetailPageURL><ItemAttributes><Actor>Brad Pitt</Actor><Actor>Eric Bana</Actor><Actor>Orlando Bloom</Actor><Director>Wolfgang Petersen</Director><Manufacturer>Warner Home Video</Manufacturer><ProductGroup>DVD</ProductGroup><Title>Troy - Director's Cut [HD DVD]</Title></ItemAttributes></Item><Item><ASIN>B000093FLA</ASIN><DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B000093FLA%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B000093FLA%253FSubscriptionId=06NSVBP9J0TT6AXYGER2</DetailPageURL><ItemAttributes><Actor>Ade</Actor><Actor>William Beck (II)</Actor><Actor>Andy Beckwith</Actor><Actor>Ewen Bremner</Actor><Actor>Jason Buckham</Actor><Creator Role="Primary Contributor">Statham, Jason</Creator><Manufacturer>Sony Pictures</Manufacturer><ProductGroup>DVD</ProductGroup><Title>Snatch (Widescreen Edition)</Title></ItemAttributes></Item><Item><ASIN>B0007P0XBO</ASIN><DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B0007P0XBO%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B0007P0XBO%253FSubscriptionId=06NSVBP9J0TT6AXYGER2</DetailPageURL><ItemAttributes><Actor>Brad Pitt</Actor><Actor>Catherine Zeta-Jones</Actor><Actor>George Clooney</Actor><Actor>Ed Kross</Actor><Actor>Julia Roberts</Actor><Director>Steven Soderbergh</Director><Manufacturer>Warner Home Video</Manufacturer><ProductGroup>DVD</ProductGroup><Title>Ocean's Twelve</Title></ItemAttributes></Item><Item><ASIN>B00004WG2F</ASIN><DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B00004WG2F%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B00004WG2F%253FSubscriptionId=06NSVBP9J0TT6AXYGER2</DetailPageURL><ItemAttributes><Actor>Brad Pitt</Actor><Actor>Anthony Hopkins</Actor><Actor>Aidan Quinn</Actor><Actor>Julia Ormond</Actor><Actor>Henry Thomas</Actor><Director>Edward Zwick</Director><Manufacturer>Sony Pictures</Manufacturer><ProductGroup>DVD</ProductGroup><Title>Legends of the Fall (Special Edition)</Title></ItemAttributes></Item><Item><ASIN>B000MCH5P4</ASIN><DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B000MCH5P4%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B000MCH5P4%253FSubscriptionId=06NSVBP9J0TT6AXYGER2</DetailPageURL><ItemAttributes><Actor>Brad Pitt</Actor><Actor>Cate Blanchett</Actor><Actor>Mohamed Akhzam</Actor><Actor>Peter Wight</Actor><Actor>Harriet Walter</Actor><Director>Alejandro González Iñárritu</Director><Manufacturer>Paramount</Manufacturer><ProductGroup>DVD</ProductGroup><Title>Babel</Title></ItemAttributes></Item><Item><ASIN>B00004RFFS</ASIN><DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=B00004RFFS%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/B00004RFFS%253FSubscriptionId=06NSVBP9J0TT6AXYGER2</DetailPageURL><ItemAttributes><Actor>Tom Cruise</Actor><Actor>Brad Pitt</Actor><Actor>Kirsten Dunst</Actor><Actor>Stephen Rea</Actor><Actor>Antonio Banderas</Actor><Director>Neil Jordan</Director><Manufacturer>Warner Home Video</Manufacturer><ProductGroup>DVD</ProductGroup><Title>Interview with the Vampire</Title></ItemAttributes></Item></Items></ItemSearchResponse>

PHP Parsing Page

<?php
// send request
$request = 'http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&' .
'AWSAccessKeyId=06NSVBP9J0TT6AXYGER2&Operation=ItemSearch&SearchIndex=DVD&Actor=Brad%20Pitt';
// get data from request
$response = file_get_contents($request);
// create temporary xml file based off data retrieved
$fp = fopen("temp.xml", "w");
fwrite($fp, $response);
fclose($fp);
// parse/display the xml
$file = 'temp.xml';

if (file_exists($file)) {
    $xml = simplexml_load_file('temp.xml');
    echo $xml->getName() . "<br />";
    foreach($xml->children() as $child) {
        echo $child->getName() . ": " . $child . "<br />";
    }
}

?>

It returns the names of the elements as it should, but not the "data" that's suppose to be within them.

Am I doing something wrong here.

The xml file changes obviously each time, but that is the basic format that is always returned.

Ah, to do multi levels inside an xml file using "simple xml" you do something like this.

 

<?php
// send request
$request = 'http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&' .
'AWSAccessKeyId=06NSVBP9J0TT6AXYGER2&Operation=ItemSearch&SearchIndex=DVD&Actor=Brad%20Pitt';
// get data from request
$response = file_get_contents($request);
// create temporary xml file based off data retrieved
$fp = fopen("temp.xml", "w");
fwrite($fp, $response);
fclose($fp);
// parse/display the xml
$file = 'temp.xml';

if (file_exists($file)) {
    $xml = simplexml_load_file('temp.xml');
    echo $xml->getName() . "<br />";
    foreach($xml->children() as $child) {
        foreach($child->children() as $childsecond) {
            foreach($childsecond->children() as $childthird) {
                echo $childthird->getName() . ": " . $childthird . "<br />";
            }
        }
    }
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.