Retrieving Data From XML in PHP
Get the solution ↓↓↓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 & 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 *** <a target="_JumpPage" href="http://cgi.ebay.co.uk/ws/eBayISAPI.dll?ViewJumpPageForVehicleHistory&vrm=Uk41MkVWUg%3D%3D&vrm_d=**52 ***">Get the Vehicle Status Report </A><img src="http://pics.ebaystatic.com/aw/pics/uk/motors/vsr_logo_small.gif" width="20" height="20" alt="VSR" align="middle" border="0" ></Value>
</NameValueList>
</ItemSpecifics>
<Country>GB</Country>
<AutoPay>false</AutoPay>
<ConditionID>3000</ConditionID>
<ConditionDisplayName>Used</ConditionDisplayName>
</Item>
</GetSingleItemResponse>
Answer
Solution:
You don't call it by itsValue
(Mileage & Colour). You call it by itsKey
Item->ItemSpecifics[0]->Name
should return{-code-4}
Item->ItemSpecifics[0]->Value
should return4
Answer
Solution:
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(thexmlns="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 valueColour
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;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: warning: undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.