0

I am having trouble accessing some of the nested data in the XML file. I can get data such as 'ItemID' by using Item->ItemID but I cannot get the data inside 'ItemSpecifics'.

For example, how would I access Colour or Mileage in ItemSpecifics?

I have tried Item->ItemSpecifics->Mileage and Item->ItemSpecifics[1]->Colour with no luck.

This is what the XML looks like:

<?xml version="1.0" encoding="UTF-8"?> <GetSingleItemResponse xmlns="urn:ebay:apis:eBLBaseComponents"> <Timestamp>2020-04-13T11:05:22.064Z</Timestamp> <Ack>Success</Ack> <Build>E1141_CORE_APILW_19170841_R1</Build> <Version>1141</Version> <Item> <ItemID>283814879195</ItemID> <EndTime>2020-04-14T13:58:36.000Z</EndTime> <ViewItemURLForNaturalSearch>https://www.ebay.co.uk/itm/MERCEDES-CL-CL55-Auto-CL55-AMG-Black-Semi-Auto-Petrol-2003-/283814879195</ViewItemURLForNaturalSearch> <ListingType>LeadGeneration</ListingType> <Location>Swindon</Location> <GalleryURL>https://thumbs4.ebaystatic.com/pict/2838148791958080_1.jpg</GalleryURL> <PictureURL>https://i.ebayimg.com/00/s/MTEyNVgxNTAw/z/XrkAAOSwO5Vd8Qph/$_1.JPG?set_id=880000500F</PictureURL> <PictureURL>https://i.ebayimg.com/00/s/MTEyNVgxNTAw/z/pasAAOSwJkFd8Qpg/$_1.JPG?set_id=880000500F</PictureURL> <PictureURL>https://i.ebayimg.com/00/s/MTEyNVgxNTAw/z/5lcAAOSwBi5d8Qph/$_1.JPG?set_id=880000500F</PictureURL> <PictureURL>https://i.ebayimg.com/00/s/MTEyNVgxNTAw/z/~dYAAOSwK~td8Qph/$_1.JPG?set_id=880000500F</PictureURL> <PictureURL>https://i.ebayimg.com/00/s/MTEyNVgxNTAw/z/xHsAAOSw3t1d8Qpg/$_1.JPG?set_id=880000500F</PictureURL> <PictureURL>https://i.ebayimg.com/00/s/MTEyNVgxNTAw/z/60wAAOSwWEdd8Qpg/$_1.JPG?set_id=880000500F</PictureURL> <PictureURL>https://i.ebayimg.com/00/s/MTEyNVgxNTAw/z/A3cAAOSwhYNd8Qpg/$_1.JPG?set_id=880000500F</PictureURL> <PrimaryCategoryID>9855</PrimaryCategoryID> <PrimaryCategoryName>Cars, Motorcycles &amp; Vehicles:Cars:Mercedes-Benz</PrimaryCategoryName> <BidCount>0</BidCount> <ConvertedCurrentPrice currencyID="GBP">4495.0</ConvertedCurrentPrice> <ListingStatus>Active</ListingStatus> <TimeLeft>P1DT2H53M14S</TimeLeft> <Title>MERCEDES CL CL55 Auto CL55 AMG Black Semi-Auto Petrol, 2003 </Title> <ItemSpecifics> <NameValueList> <Name>Previous owners</Name> <Value>4</Value> </NameValueList> <NameValueList> <Name>Colour</Name> <Value>Black</Value> </NameValueList> <NameValueList> <Name>Mileage</Name> <Value>146951</Value> </NameValueList> <NameValueList> <Name>Power</Name> <Value>360</Value> </NameValueList> <NameValueList> <Name>Engine Size</Name> <Value>5439</Value> </NameValueList> <NameValueList> <Name>Year</Name> <Value>2003</Value> </NameValueList> <NameValueList> <Name>Manufacturer</Name> <Value>Mercedes-Benz</Value> </NameValueList> <NameValueList> <Name>Model</Name> <Value>Other</Value> </NameValueList> <NameValueList> <Name>Body Type</Name> <Value>Other</Value> </NameValueList> <NameValueList> <Name>Doors</Name> <Value>2</Value> </NameValueList> <NameValueList> <Name>Seats</Name> <Value>4</Value> </NameValueList> <NameValueList> <Name>Transmission</Name> <Value>Semi-Automatic</Value> </NameValueList> <NameValueList> <Name>Fuel</Name> <Value>Petrol</Value> </NameValueList> <NameValueList> <Name>Reg. Date</Name> <Value>20030114</Value> </NameValueList> <NameValueList> <Name>MOT Expiry</Name> <Value>202006</Value> </NameValueList> <NameValueList> <Name>Reg. Mark</Name> <Value>**52 *** &lt;a target=&quot;_JumpPage&quot; href=&quot;http://cgi.ebay.co.uk/ws/eBayISAPI.dll?ViewJumpPageForVehicleHistory&amp;vrm=Uk41MkVWUg%3D%3D&amp;vrm_d=**52 ***&quot;&gt;Get the Vehicle Status Report &lt;/A&gt;&lt;img src=&quot;http://pics.ebaystatic.com/aw/pics/uk/motors/vsr_logo_small.gif&quot; width=&quot;20&quot; height=&quot;20&quot; alt=&quot;VSR&quot; align=&quot;middle&quot; border=&quot;0&quot; &gt;</Value> </NameValueList> </ItemSpecifics> <Country>GB</Country> <AutoPay>false</AutoPay> <ConditionID>3000</ConditionID> <ConditionDisplayName>Used</ConditionDisplayName> </Item> </GetSingleItemResponse> 
1
  • 1
    mileage or colour are xml values, you can't access them like that ; to search for some values for a given tag (here 'name'), you have to use a xpath search. See PHP manual for xpath and SimpleXml Commented Apr 13, 2020 at 11:55

2 Answers 2

3

You don't call it by its Value (Mileage & Colour). You call it by its Key

Item->ItemSpecifics[0]->Name should return Previous owners
Item->ItemSpecifics[0]->Value should return 4

Sign up to request clarification or add additional context in comments.

1 Comment

This answers how to access the name and values, but not how to get the Colour value.
1

As the colour is in a list of name/value elements, you would be better off using XPath to search for the correct pair. As the XML has a default namespace(the xmlns="urn:ebay:apis:eBLBaseComponents" bit), you also have to register that to be able to use a prefix in your XPath.

This uses //d:NameValueList[d:Name="Colour"], but it is looking for a element with the value Colour in the <NameValueList> list. xpath() always returns a list of matches, so use [0] to get the first one. This also gives you the <NameValueList> item, so use $colour[0]->Value...

$xml = simplexml_load_file($fileName); $xml->registerXPathNamespace("d", "urn:ebay:apis:eBLBaseComponents"); $colour = $xml->xpath('//d:ItemSpecifics/d:NameValueList[d:Name="Colour"]'); echo $colour[0]->Value; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.