XML 使用 simplexml 以 PHP 解析
XML Parsing with PHP using simplexml
我正在使用亚马逊商城网络服务 (Amazon MWS) 来获取产品。
我需要获取第一个产品的标题:
对于 ASIN,我可以使用:
$xml->ListMatchingProductsResult->Products->Product[0]->Identifiers->MarketplaceASIN->ASIN
但是对于标题,我不知道如何,因为 "ns2"。
这是 XML 数据:
<?xml version="1.0"?>
<ListMatchingProductsResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<ListMatchingProductsResult>
<Products>
<Product>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>A2EUQ1WTGCTBG2</MarketplaceId>
<ASIN>B01MYV7W6A</ASIN>
</MarketplaceASIN>
</Identifiers>
<AttributeSets>
<ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="en-CA">
<ns2:Binding>Automotive</ns2:Binding>
<ns2:Brand>Multiple Manufacturers</ns2:Brand>
<ns2:ItemDimensions>
<ns2:Weight Units="pounds">12</ns2:Weight>
</ns2:ItemDimensions>
<ns2:Label>Multiple Manufacturers</ns2:Label>
<ns2:Manufacturer>Multiple Manufacturers</ns2:Manufacturer>
<ns2:Model>AC1000149R</ns2:Model>
<ns2:PackageDimensions>
<ns2:Height Units="inches">18</ns2:Height>
<ns2:Length Units="inches">78</ns2:Length>
<ns2:Width Units="inches">38</ns2:Width>
<ns2:Weight Units="pounds">12</ns2:Weight>
</ns2:PackageDimensions>
<ns2:PackageQuantity>1</ns2:PackageQuantity>
<ns2:PartNumber>AC1000149R</ns2:PartNumber>
<ns2:ProductGroup>Automotive Parts and Accessories</ns2:ProductGroup>
<ns2:ProductTypeName>AUTO_ACCESSORY</ns2:ProductTypeName>
<ns2:Publisher>Multiple Manufacturers</ns2:Publisher>
<ns2:SmallImage>
<ns2:URL>https://m.media-amazon.com/images/I/31gfzCbfPRL._SL75_.jpg</ns2:URL>
<ns2:Height Units="pixels">75</ns2:Height>
<ns2:Width Units="pixels">75</ns2:Width>
</ns2:SmallImage>
<ns2:Studio>Multiple Manufacturers</ns2:Studio>
<ns2:Title>OE Replacement 2004-2006 Acura TL Bumper Cover (Partslink Number AC1000149)</ns2:Title>
</ns2:ItemAttributes>
</AttributeSets>
<Relationships/>
<SalesRankings/>
</Product>
</Products>
</ListMatchingProductsResult>
<ResponseMetadata>
<RequestId>83360543-e16c-4c0f-a4b5-5dcca89a3075</RequestId>
</ResponseMetadata>
</ListMatchingProductsResponse>
提前致谢。
这是我找到的解决方案:
$ItemAttributes = $xml->ListMatchingProductsResult->Products->Product[$i]->AttributeSets->ItemAttributes;
$ItemAttributes->registerXPathNamespace('ns2', 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd');
$titles = $ItemAttributes->xpath("//ns2:Title"); // <= this is array
我正在使用亚马逊商城网络服务 (Amazon MWS) 来获取产品。
我需要获取第一个产品的标题:
对于 ASIN,我可以使用:
$xml->ListMatchingProductsResult->Products->Product[0]->Identifiers->MarketplaceASIN->ASIN
但是对于标题,我不知道如何,因为 "ns2"。
这是 XML 数据:
<?xml version="1.0"?>
<ListMatchingProductsResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<ListMatchingProductsResult>
<Products>
<Product>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>A2EUQ1WTGCTBG2</MarketplaceId>
<ASIN>B01MYV7W6A</ASIN>
</MarketplaceASIN>
</Identifiers>
<AttributeSets>
<ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="en-CA">
<ns2:Binding>Automotive</ns2:Binding>
<ns2:Brand>Multiple Manufacturers</ns2:Brand>
<ns2:ItemDimensions>
<ns2:Weight Units="pounds">12</ns2:Weight>
</ns2:ItemDimensions>
<ns2:Label>Multiple Manufacturers</ns2:Label>
<ns2:Manufacturer>Multiple Manufacturers</ns2:Manufacturer>
<ns2:Model>AC1000149R</ns2:Model>
<ns2:PackageDimensions>
<ns2:Height Units="inches">18</ns2:Height>
<ns2:Length Units="inches">78</ns2:Length>
<ns2:Width Units="inches">38</ns2:Width>
<ns2:Weight Units="pounds">12</ns2:Weight>
</ns2:PackageDimensions>
<ns2:PackageQuantity>1</ns2:PackageQuantity>
<ns2:PartNumber>AC1000149R</ns2:PartNumber>
<ns2:ProductGroup>Automotive Parts and Accessories</ns2:ProductGroup>
<ns2:ProductTypeName>AUTO_ACCESSORY</ns2:ProductTypeName>
<ns2:Publisher>Multiple Manufacturers</ns2:Publisher>
<ns2:SmallImage>
<ns2:URL>https://m.media-amazon.com/images/I/31gfzCbfPRL._SL75_.jpg</ns2:URL>
<ns2:Height Units="pixels">75</ns2:Height>
<ns2:Width Units="pixels">75</ns2:Width>
</ns2:SmallImage>
<ns2:Studio>Multiple Manufacturers</ns2:Studio>
<ns2:Title>OE Replacement 2004-2006 Acura TL Bumper Cover (Partslink Number AC1000149)</ns2:Title>
</ns2:ItemAttributes>
</AttributeSets>
<Relationships/>
<SalesRankings/>
</Product>
</Products>
</ListMatchingProductsResult>
<ResponseMetadata>
<RequestId>83360543-e16c-4c0f-a4b5-5dcca89a3075</RequestId>
</ResponseMetadata>
</ListMatchingProductsResponse>
提前致谢。
这是我找到的解决方案:
$ItemAttributes = $xml->ListMatchingProductsResult->Products->Product[$i]->AttributeSets->ItemAttributes;
$ItemAttributes->registerXPathNamespace('ns2', 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd');
$titles = $ItemAttributes->xpath("//ns2:Title"); // <= this is array