skip to Main Content

Trying to parse using SimpleXML function, which is working fine. But I’ve been stuck on how to extract the ‘colour’ & ‘length data from the XML

Below is snippet of the ‘ebay-response.xml’ file referenced in the code:

Complete xml file can be downloaded from 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) for 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 to load XML Stream from eBAY API, possible no response from eBay?<br />n";
    return;
}

if ($resp->Ack != "Success") {
echo 'eBay Response Status was: ' . $resp->Ack . " Unable to parse the 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() as $SkuAndQuantity )
{
echo 'Title: ' . $resp->Item->Title . ' SKU: ' . $SkuAndQuantity->SKU .
         ' Qty: ' . $SkuAndQuantity->Quantity . "<br />n";
    foreach( $resp->Item->Variations->Variation->VariationSpecifics->NameValueList->children() as $options )
{
echo $options .'<br />';
}
}
echo "<br />n";

What I get back when I run my code is shown below, as you can see I appear to be pulling back only colour white (and no length) and even with the colour I appear to be only getting the colour from the 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) for Skirt Trousers Craft
Total Items (all variations): 25000

Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3448 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3449 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3450 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3451 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3452 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3453 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3454 Qty: 100
Colour

2

Answers


  1. This part of code will not iterate through Variation or NameValueList elements:

    foreach($resp->Item->Variations->Variation->VariationSpecifics->NameValueList->children() as $options)
    {
      echo $options .'<br />';
    } 
    

    It will always use the first Variation element and first NameValueList element inside. This causes your problem.

    You need to change your code to something like:

    foreach($resp->Item->Variations->children() as $Variation)
    {
      echo 'Title: ' . $resp->Item->Title . ' SKU: ' . $Variation->SKU .' Qty: ' . $Variation->Quantity .'<br />';
      foreach($Variation->VariationSpecifics->children() as $NameValueList)
        foreach($NameValueList->children() as $option)
          echo $option .'<br />';           
    }
    
    Login or Signup to reply.
  2. XML allows to represent data in a hierarchical fashion, also as some kind of tree.

    In the ebay answer you have one <Item> element.

    That <Item> element can have one <Variations> element.

    That <Variations> element can have zero or more <Variation> elements.

    The <Variantion> elements then again can have <Name>/<Value> pairs inside zero or more <VariationSpecifics> <NameValueList> elements.

    <GetItemResponse>
      <Item> (1)
        <Variations> (1)
          <Variation> (1..n)
            <VariationSpecifics> (1)
              <NameValueList>  (1..n)
                <Name> (1)
                <Value> (1)
    

    For the elements that can have more than one entry (1..n) you need to do iterations over.

    The following is an example of such an iteration for plain text output (as it’s easier for demonstration purposes):

    $xml = simplexml_load_file('ebay-response.xml');
    $item = $xml->Item; // one <Item>
    $variations = $item->Variations->Variation; // one or more <Variation>
    
    echo $item->Title, "n";
    echo str_repeat('=', strlen($item->Title)), "n";
    
    foreach ($variations as $variation) {
        echo " * ";
        $nameValues = new CachingIterator(new IteratorIterator($variation->VariationSpecifics->NameValueList));
        foreach ($nameValues as $pair) {
            echo $pair->Name, ': ', $pair->Value, $nameValues->hasNext() ? '; ' : ' ';
        }
        echo '(SKU: ', $variation->SKU, ')', "n";
    }
    

    It shows that you need to stack multiple foreach’es into each other. Here those are two – because there are two elements marked with (1..n) in the schema above.

    The inner foreach is a bit special because I wrapped the <NameValueList> elements inside a CachingIterator. That one adds a nice method you can ask if there will be a next element in the foreach loop ($nameValues->hasNext()) which is used in the example to separate the name/value pairs with a semicolon (“;“).

    The exemplary (shortened) output is:

    CLOSED END ZIPS 40 Colours 4  6  8 10 12in (10-30cm) for Skirt Trousers Craft
    =============================================================================
     * Colour: White; Length: 4" (10cm) (SKU: 1176:3448)
     * Colour: White; Length: 5" (12cm) (SKU: 1176:3449)
     * Colour: White; Length: 6" (15cm) (SKU: 1176:3450)
    ...
     * Colour: Dark Grey; Length: 6" (15cm) (SKU: 1213:3450)
     * Colour: Dark Grey; Length: 7" (18cm) (SKU: 1213:3451)
     * Colour: Dark Grey; Length: 8" (20cm) (SKU: 1213:3452)
    

    Now this is not always the display you want here. For example you might want to list all colours and for each colour you want to show the available sizes. This will become overly complicated with foreach’ing over the SimpleXMLElements you have there. An alternative to foreach is running one or more xpath queries over which’s result you can foreach again.

    Here is another example that lists alls lengths by their colors:

    $item->registerXPathNamespace('xs', 'urn:ebay:apis:eBLBaseComponents');
    $colors  = $item->xpath('xs:Variations//xs:Value[preceding-sibling::xs:Name = "Colour"]');
    $colors  = array_unique(array_map('trim', $colors));
    
    foreach($colors as $color) {
        echo " * Color ", $color, ": ";
        $colorLengths = $item->xpath(sprintf(
            '  xs:Variations
                 //xs:Value [. = %s and preceding-sibling::xs:Name = "Colour"]
             /../..//xs:Value [preceding-sibling::xs:Name = "Length"]', xpath_string($color)));
        $count = count($colorLengths);
        echo "$count Lengths: ";
        foreach ($colorLengths as $i => $length) {
            $hasNext = $count - $i - 1;
            $length->registerXPathNamespace('xs', 'urn:ebay:apis:eBLBaseComponents');
            echo $length, ' (SKU: ', $length->xpath('../../../xs:SKU')[0], ')', $hasNext ? '; ' : '';
        }
        echo "n";
    }
    

    Exemplary output:

    CLOSED END ZIPS 40 Colours 4  6  8 10 12in (10-30cm) for Skirt Trousers Craft
    =============================================================================
     * Color White: 7 Lengths: 4" (10cm) (SKU: 1176:3448); 5" (12cm) (SKU: 1176:3449); 6" (15cm) (SKU: 1176:3450); 7" (18cm) (SKU: 1176:3451); 8" (20cm) (SKU: 1176:3452); 10" (25cm) (SKU: 1176:3453); 12" (30cm) (SKU: 1176:3454)
     * Color Ivory: 7 Lengths: 4" (10cm) (SKU: 1177:3448); 5" (12cm) (SKU: 1177:3449); 6" (15cm) (SKU: 1177:3450); 7" (18cm) (SKU: 1177:3451); 8" (20cm) (SKU: 1177:3452); 10" (25cm) (SKU: 1177:3453); 12" (30cm) (SKU: 1177:3454)
     * Color Cream: 7 Lengths: 4" (10cm) (SKU: 1178:3448); 5" (12cm) (SKU: 1178:3449); 6" (15cm) (SKU: 1178:3450); 7" (18cm) (SKU: 1178:3451); 8" (20cm) (SKU: 1178:3452); 10" (25cm) (SKU: 1178:3453); 12" (30cm) (SKU: 1178:3454)
     * Color Lemon: 7 Lengths: 4" (10cm) (SKU: 1179:3448); 5" (12cm) (SKU: 1179:3449); 6" (15cm) (SKU: 1179:3450); 7" (18cm) (SKU: 1179:3451); 8" (20cm) (SKU: 1179:3452); 10" (25cm) (SKU: 1179:3453); 12" (30cm) (SKU: 1179:3454)
     * Color Peach: 7 Lengths: 4" (10cm) (SKU: 1180:3448); 5" (12cm) (SKU: 1180:3449); 6" (15cm) (SKU: 1180:3450); 7" (18cm) (SKU: 1180:3451); 8" (20cm) (SKU: 1180:3452); 10" (25cm) (SKU: 1180:3453); 12" (30cm) (SKU: 1180:3454)
     * Color Pale Green: 7 Lengths: 4" (10cm) (SKU: 1181:3448); 5" (12cm) (SKU: 1181:3449); 6" (15cm) (SKU: 1181:3450); 7" (18cm) (SKU: 1181:3451); 8" (20cm) (SKU: 1181:3452); 10" (25cm) (SKU: 1181:3453); 12" (30cm) (SKU: 1181:3454)
     * Color Aqua: 7 Lengths: 4" (10cm) (SKU: 1182:3448); 5" (12cm) (SKU: 1182:3449); 6" (15cm) (SKU: 1182:3450); 7" (18cm) (SKU: 1182:3451); 8" (20cm) (SKU: 1182:3452); 10" (25cm) (SKU: 1182:3453); 12" (30cm) (SKU: 1182:3454)
     * Color Pale Blue: 7 Lengths: 4" (10cm) (SKU: 1183:3448); 5" (12cm) (SKU: 1183:3449); 6" (15cm) (SKU: 1183:3450); 7" (18cm) (SKU: 1183:3451); 8" (20cm) (SKU: 1183:3452); 10" (25cm) (SKU: 1183:3453); 12" (30cm) (SKU: 1183:3454)
     * Color Mid Blue: 7 Lengths: 4" (10cm) (SKU: 1184:3448); 5" (12cm) (SKU: 1184:3449); 6" (15cm) (SKU: 1184:3450); 7" (18cm) (SKU: 1184:3451); 8" (20cm) (SKU: 1184:3452); 10" (25cm) (SKU: 1184:3453); 12" (30cm) (SKU: 1184:3454)
     * Color Lilac: 7 Lengths: 4" (10cm) (SKU: 1185:3448); 5" (12cm) (SKU: 1185:3449); 6" (15cm) (SKU: 1185:3450); 7" (18cm) (SKU: 1185:3451); 8" (20cm) (SKU: 1185:3452); 10" (25cm) (SKU: 1185:3453); 12" (30cm) (SKU: 1185:3454)
     * Color Heather: 7 Lengths: 4" (10cm) (SKU: 1186:3448); 5" (12cm) (SKU: 1186:3449); 6" (15cm) (SKU: 1186:3450); 7" (18cm) (SKU: 1186:3451); 8" (20cm) (SKU: 1186:3452); 10" (25cm) (SKU: 1186:3453); 12" (30cm) (SKU: 1186:3454)
     * Color Pale Pink: 7 Lengths: 4" (10cm) (SKU: 1187:3448); 5" (12cm) (SKU: 1187:3449); 6" (15cm) (SKU: 1187:3450); 7" (18cm) (SKU: 1187:3451); 8" (20cm) (SKU: 1187:3452); 10" (25cm) (SKU: 1187:3453); 12" (30cm) (SKU: 1187:3454)
     * Color Vintage Pink: 7 Lengths: 4" (10cm) (SKU: 1189:3448); 5" (12cm) (SKU: 1189:3449); 6" (15cm) (SKU: 1189:3450); 7" (18cm) (SKU: 1189:3451); 8" (20cm) (SKU: 1189:3452); 10" (25cm) (SKU: 1189:3453); 12" (30cm) (SKU: 1189:3454)
     * Color Ruby: 7 Lengths: 4" (10cm) (SKU: 1190:3448); 5" (12cm) (SKU: 1190:3449); 6" (15cm) (SKU: 1190:3450); 7" (18cm) (SKU: 1190:3451); 8" (20cm) (SKU: 1190:3452); 10" (25cm) (SKU: 1190:3453); 12" (30cm) (SKU: 1190:3454)
     * Color Cerise: 7 Lengths: 4" (10cm) (SKU: 1191:3448); 5" (12cm) (SKU: 1191:3449); 6" (15cm) (SKU: 1191:3450); 7" (18cm) (SKU: 1191:3451); 8" (20cm) (SKU: 1191:3452); 10" (25cm) (SKU: 1191:3453); 12" (30cm) (SKU: 1191:3454)
     * Color Bright Purple: 7 Lengths: 4" (10cm) (SKU: 1193:3448); 5" (12cm) (SKU: 1193:3449); 6" (15cm) (SKU: 1193:3450); 7" (18cm) (SKU: 1193:3451); 8" (20cm) (SKU: 1193:3452); 10" (25cm) (SKU: 1193:3453); 12" (30cm) (SKU: 1193:3454)
     * Color Deep Purple: 7 Lengths: 4" (10cm) (SKU: 1194:3448); 5" (12cm) (SKU: 1194:3449); 6" (15cm) (SKU: 1194:3450); 7" (18cm) (SKU: 1194:3451); 8" (20cm) (SKU: 1194:3452); 10" (25cm) (SKU: 1194:3453); 12" (30cm) (SKU: 1194:3454)
     * Color Magenta: 7 Lengths: 4" (10cm) (SKU: 1192:3448); 5" (12cm) (SKU: 1192:3449); 6" (15cm) (SKU: 1192:3450); 7" (18cm) (SKU: 1192:3451); 8" (20cm) (SKU: 1192:3452); 10" (25cm) (SKU: 1192:3453); 12" (30cm) (SKU: 1192:3454)
     * Color Bright Red: 7 Lengths: 4" (10cm) (SKU: 1195:3448); 5" (12cm) (SKU: 1195:3449); 6" (15cm) (SKU: 1195:3450); 7" (18cm) (SKU: 1195:3451); 8" (20cm) (SKU: 1195:3452); 10" (25cm) (SKU: 1195:3453); 12" (30cm) (SKU: 1195:3454)
     * Color Cherry Red: 7 Lengths: 4" (10cm) (SKU: 1196:3448); 5" (12cm) (SKU: 1196:3449); 6" (15cm) (SKU: 1196:3450); 7" (18cm) (SKU: 1196:3451); 8" (20cm) (SKU: 1196:3452); 10" (25cm) (SKU: 1196:3453); 12" (30cm) (SKU: 1196:3454)
     * Color Orange: 7 Lengths: 4" (10cm) (SKU: 1197:3448); 5" (12cm) (SKU: 1197:3449); 6" (15cm) (SKU: 1197:3450); 7" (18cm) (SKU: 1197:3451); 8" (20cm) (SKU: 1197:3452); 10" (25cm) (SKU: 1197:3453); 12" (30cm) (SKU: 1197:3454)
     * Color Gold: 7 Lengths: 4" (10cm) (SKU: 1198:3448); 5" (12cm) (SKU: 1198:3449); 6" (15cm) (SKU: 1198:3450); 7" (18cm) (SKU: 1198:3451); 8" (20cm) (SKU: 1198:3452); 10" (25cm) (SKU: 1198:3453); 12" (30cm) (SKU: 1198:3454)
     * Color Yellow: 7 Lengths: 4" (10cm) (SKU: 1199:3448); 5" (12cm) (SKU: 1199:3449); 6" (15cm) (SKU: 1199:3450); 7" (18cm) (SKU: 1199:3451); 8" (20cm) (SKU: 1199:3452); 10" (25cm) (SKU: 1199:3453); 12" (30cm) (SKU: 1199:3455)
     * Color Bright Lime: 7 Lengths: 4" (10cm) (SKU: 1200:3448); 5" (12cm) (SKU: 1200:3449); 6" (15cm) (SKU: 1200:3450); 7" (18cm) (SKU: 1200:3451); 8" (20cm) (SKU: 1200:3452); 10" (25cm) (SKU: 1200:3453); 12" (30cm) (SKU: 1200:3454)
     * Color Emerald Green: 7 Lengths: 4" (10cm) (SKU: 1202:3448); 5" (12cm) (SKU: 1202:3449); 6" (15cm) (SKU: 1202:3450); 7" (18cm) (SKU: 1202:3451); 8" (20cm) (SKU: 1202:3452); 10" (25cm) (SKU: 1202:3453); 12" (30cm) (SKU: 1202:3454)
     * Color Forest Green: 7 Lengths: 4" (10cm) (SKU: 1203:3448); 5" (12cm) (SKU: 1203:3449); 6" (15cm) (SKU: 1203:3450); 7" (18cm) (SKU: 1203:3451); 8" (20cm) (SKU: 1203:3452); 10" (25cm) (SKU: 1203:3453); 12" (30cm) (SKU: 1203:3454)
     * Color Olive Green: 7 Lengths: 4" (10cm) (SKU: 1204:3448); 5" (12cm) (SKU: 1204:3449); 6" (15cm) (SKU: 1204:3450); 7" (18cm) (SKU: 1204:3451); 8" (20cm) (SKU: 1204:3452); 10" (25cm) (SKU: 1204:3453); 12" (30cm) (SKU: 1204:3454)
     * Color Sage Green: 7 Lengths: 4" (10cm) (SKU: 1201:3448); 5" (12cm) (SKU: 1201:3449); 6" (15cm) (SKU: 1201:3450); 7" (18cm) (SKU: 1201:3451); 8" (20cm) (SKU: 1201:3452); 10" (25cm) (SKU: 1201:3453); 12" (30cm) (SKU: 1201:3454)
     * Color Turquoise: 7 Lengths: 4" (10cm) (SKU: 1205:3448); 5" (12cm) (SKU: 1205:3449); 6" (15cm) (SKU: 1205:3450); 7" (18cm) (SKU: 1205:3451); 8" (20cm) (SKU: 1205:3452); 10" (25cm) (SKU: 1205:3453); 12" (30cm) (SKU: 1205:3454)
     * Color Denim Blue: 7 Lengths: 4" (10cm) (SKU: 1207:3448); 5" (12cm) (SKU: 1207:3449); 6" (15cm) (SKU: 1207:3450); 7" (18cm) (SKU: 1207:3451); 8" (20cm) (SKU: 1207:3452); 10" (25cm) (SKU: 1207:3453); 12" (30cm) (SKU: 1207:3454)
     * Color Royal Blue: 7 Lengths: 4" (10cm) (SKU: 1206:3448); 5" (12cm) (SKU: 1206:3449); 6" (15cm) (SKU: 1206:3450); 7" (18cm) (SKU: 1206:3451); 8" (20cm) (SKU: 1206:3452); 10" (25cm) (SKU: 1206:3453); 12" (30cm) (SKU: 1206:3454)
     * Color Navy: 7 Lengths: 4" (10cm) (SKU: 1208:3448); 5" (12cm) (SKU: 1208:3449); 6" (15cm) (SKU: 1208:3450); 7" (18cm) (SKU: 1208:3451); 8" (20cm) (SKU: 1208:3452); 10" (25cm) (SKU: 1208:3453); 12" (30cm) (SKU: 1208:3454)
     * Color Rust: 7 Lengths: 4" (10cm) (SKU: 1210:3448); 5" (12cm) (SKU: 1210:3449); 6" (15cm) (SKU: 1210:3450); 7" (18cm) (SKU: 1210:3451); 8" (20cm) (SKU: 1210:3452); 10" (25cm) (SKU: 1210:3453); 12" (30cm) (SKU: 1210:3454)
     * Color Dark Brown: 7 Lengths: 4" (10cm) (SKU: 1211:3448); 5" (12cm) (SKU: 1211:3449); 6" (15cm) (SKU: 1211:3450); 7" (18cm) (SKU: 1211:3451); 8" (20cm) (SKU: 1211:3452); 10" (25cm) (SKU: 1211:3453); 12" (30cm) (SKU: 1211:3454)
     * Color Light Grey: 7 Lengths: 4" (10cm) (SKU: 1212:3448); 5" (12cm) (SKU: 1212:3449); 6" (15cm) (SKU: 1212:3450); 7" (18cm) (SKU: 1212:3451); 8" (20cm) (SKU: 1212:3452); 10" (25cm) (SKU: 1212:3453); 12" (30cm) (SKU: 1212:3454)
     * Color Dark Grey: 5 Lengths: 4" (10cm) (SKU: 1213:3448); 5" (12cm) (SKU: 1213:3449); 6" (15cm) (SKU: 1213:3450); 7" (18cm) (SKU: 1213:3451); 8" (20cm) (SKU: 1213:3452)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search