xml - PHP SimpleXML Parsing Issue, get duplicate elements -


trying parse using simplexml function, working fine. i've been stuck on how extract 'colour' & 'length data xml

below snippet of 'ebay-response.xml' file referenced in code:

complete xml file can downloaded ebay-response.xml

<?xml version="1.0" encoding="utf-8"?> <getitemresponse xmlns="urn:ebay:apis:eblbasecomponents">   <timestamp>2015-08-03t11:45:56.061z</timestamp>   <ack>success</ack>   <version>927</version>   <build>e927_intl_api_17590342_r1</build>   <item>     <quantity>25000</quantity>     <shippingdetails />     <title>closed end zips 40 colours 4  6  8 10 12in (10-30cm) skirt trousers craft</title>     <variations>       <variation>         <sku>1176:3448</sku>         <quantity>100</quantity>         <variationspecifics>           <namevaluelist>             <name>colour</name>             <value>white</value>           </namevaluelist>           <namevaluelist>             <name>length</name>             <value>4" (10cm)</value>           </namevaluelist>         </variationspecifics>       </variation>       <variation>         <sku>1176:3449</sku>         <quantity>100</quantity>         <variationspecifics>           <namevaluelist>             <name>colour</name>             <value>white</value>           </namevaluelist>           <namevaluelist>             <name>length</name>             <value>5" (12cm)</value>           </namevaluelist>         </variationspecifics>       </variation>       <variation>         <sku>1176:3450</sku>         <quantity>100</quantity>         <variationspecifics>           <namevaluelist>             <name>colour</name>             <value>white</value>           </namevaluelist>           <namevaluelist>             <name>length</name>             <value>6" (15cm)</value>           </namevaluelist>         </variationspecifics>       </variation>       <variation>         <sku>1176:3451</sku>         <quantity>100</quantity>         <variationspecifics>           <namevaluelist>             <name>colour</name>             <value>white</value>           </namevaluelist>           <namevaluelist>             <name>length</name>             <value>7" (18cm)</value>           </namevaluelist>         </variationspecifics>       </variation>       <variation> 

my current php script is:

if(!$resp = simplexml_load_file("ebay-response.xml")) { echo "unable load xml stream ebay api, possible no response ebay?<br />\n";     return; }  if ($resp->ack != "success") { echo 'ebay response status was: ' . $resp->ack . " unable parse xml <br />\n";     return; }  echo 'ebay response status: ' . $resp->ack . "<br />\n"; echo 'ebay response timestamp: ' . $resp->timestamp . "<br />\n"; echo 'ebay api version: ' . $resp->version . "<br />\n"; echo 'ebay api build: ' . $resp->build . "<br />\n";  echo 'ebay item title: ' . $resp->item->title . "<br />\n"; echo 'total items (all variations): ' . $resp->item->quantity . "<br />\n<br />\n";  foreach( $resp->item->variations->children() $skuandquantity ) { echo 'title: ' . $resp->item->title . ' sku: ' . $skuandquantity->sku .          ' qty: ' . $skuandquantity->quantity . "<br />\n";     foreach( $resp->item->variations->variation->variationspecifics->namevaluelist->children() $options ) { echo $options .'<br />'; } } echo "<br />\n"; 

what when run code shown below, can see appear pulling colour white (and no length) , colour appear getting colour 1st 'variation' element

ebay response status: success ebay response timestamp: 2015-08-03t11:45:56.061z ebay api version: 927 ebay api build: e927_intl_api_17590342_r1 ebay item title: closed end zips 40 colours 4 6 8 10 12in (10-30cm) skirt trousers craft total items (all variations): 25000  title: closed end zips 40 colours 4 6 8 10 12in (10-30cm) skirt trousers craft sku: 1176:3448 qty: 100 colour white title: closed end zips 40 colours 4 6 8 10 12in (10-30cm) skirt trousers craft sku: 1176:3449 qty: 100 colour white title: closed end zips 40 colours 4 6 8 10 12in (10-30cm) skirt trousers craft sku: 1176:3450 qty: 100 colour white title: closed end zips 40 colours 4 6 8 10 12in (10-30cm) skirt trousers craft sku: 1176:3451 qty: 100 colour white title: closed end zips 40 colours 4 6 8 10 12in (10-30cm) skirt trousers craft sku: 1176:3452 qty: 100 colour white title: closed end zips 40 colours 4 6 8 10 12in (10-30cm) skirt trousers craft sku: 1176:3453 qty: 100 colour white title: closed end zips 40 colours 4 6 8 10 12in (10-30cm) skirt trousers craft sku: 1176:3454 qty: 100 colour 

this part of code not iterate through variation or namevaluelist elements:

foreach($resp->item->variations->variation->variationspecifics->namevaluelist->children() $options) {   echo $options .'<br />'; }  

it use first variation element , first namevaluelist element inside. causes problem.

you need change code like:

foreach($resp->item->variations->children() $variation) {   echo 'title: ' . $resp->item->title . ' sku: ' . $variation->sku .' qty: ' . $variation->quantity .'<br />';   foreach($variation->variationspecifics->children() $namevaluelist)     foreach($namevaluelist->children() $option)       echo $option .'<br />';            } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -