从 PHP 中的 XML 检索数据
Retrieving Data From XML in PHP
我无法访问 XML 文件中的某些嵌套数据。我可以使用 Item->ItemID 获取 'ItemID' 等数据,但我无法获取 'ItemSpecifics' 中的数据。
例如,我如何访问 ItemSpecifics 中的颜色或里程数?
我已经尝试了 Item->ItemSpecifics->Mileage 和 Item->ItemSpecifics[1]->Colour,但没有成功。
这就是 XML 的样子:
<?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>
您不会通过 Value
(里程和颜色)来称呼它。你用 Key
来称呼它
Item->ItemSpecifics[0]->Name
应该 return Previous owners
Item->ItemSpecifics[0]->Value
应该 return 4
由于颜色在 name/value 元素的列表中,您最好使用 XPath 来搜索正确的对。由于 XML 有一个默认命名空间(xmlns="urn:ebay:apis:eBLBaseComponents"
位),您还必须注册它才能在 XPath 中使用前缀。
这使用 //d:NameValueList[d:Name="Colour"]
,但它正在 <NameValueList>
列表中查找值为 Colour
的元素。 xpath()
始终是 returns 匹配列表,因此使用 [0]
获取第一个。这也给你 <NameValueList>
项目,所以使用 $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;
我无法访问 XML 文件中的某些嵌套数据。我可以使用 Item->ItemID 获取 'ItemID' 等数据,但我无法获取 'ItemSpecifics' 中的数据。
例如,我如何访问 ItemSpecifics 中的颜色或里程数?
我已经尝试了 Item->ItemSpecifics->Mileage 和 Item->ItemSpecifics[1]->Colour,但没有成功。
这就是 XML 的样子:
<?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>
您不会通过 Value
(里程和颜色)来称呼它。你用 Key
Item->ItemSpecifics[0]->Name
应该 return Previous owners
Item->ItemSpecifics[0]->Value
应该 return 4
由于颜色在 name/value 元素的列表中,您最好使用 XPath 来搜索正确的对。由于 XML 有一个默认命名空间(xmlns="urn:ebay:apis:eBLBaseComponents"
位),您还必须注册它才能在 XPath 中使用前缀。
这使用 //d:NameValueList[d:Name="Colour"]
,但它正在 <NameValueList>
列表中查找值为 Colour
的元素。 xpath()
始终是 returns 匹配列表,因此使用 [0]
获取第一个。这也给你 <NameValueList>
项目,所以使用 $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;