我正在尝试从亚马逊解析xml文件但遇到了困难。我使用的是simplexml_load_string
$ xml = simplexml_load_string('我的xml在这里');
但是当我做的时候
echo $ xml-> GetMatchingProductResult-> Product-> AttributeSets;
它什么都没有显示
如何访问品牌价值?
我的xml文件:
<?xml version="1.0"?>
<GetMatchingProductResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetMatchingProductResult ASIN="B003IOSNNQ" status="Success">
<Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
<ASIN>B003IOSNNQ</ASIN>
</MarketplaceASIN>
</Identifiers>
<AttributeSets>
<ns2:ItemAttributes xml:lang="en-GB">
<ns2:Binding>Misc.</ns2:Binding>
<ns2:Brand>eSecure</ns2:Brand>
<ns2:Feature>Colour: Classic Black</ns2:Feature>
<ns2:Feature>Thickness: Approximately 40mm - More than msot sellers</ns2:Feature>
<ns2:Feature>Dimensions [length x width at widest and narrowest]: approximately 280 x 190 x 70mm</ns2:Feature>
<ns2:Feature>Material: Stretch Lycra, Silica Gel</ns2:Feature>
<ns2:Feature>Ideal for Standard Bicycle / Static Exercise Bike</ns2:Feature>
<ns2:Label>eSecure</ns2:Label>
<ns2:Manufacturer>eSecure</ns2:Manufacturer>
<ns2:PackageDimensions>
<ns2:Height Units="inches">2.13</ns2:Height>
<ns2:Length Units="inches">10.71</ns2:Length>
<ns2:Width Units="inches">7.72</ns2:Width>
<ns2:Weight Units="pounds">0.49</ns2:Weight>
</ns2:PackageDimensions>
<ns2:PackageQuantity>1</ns2:PackageQuantity>
<ns2:ProductGroup>Sports</ns2:ProductGroup>
<ns2:ProductTypeName>SPORTING_GOODS</ns2:ProductTypeName>
<ns2:Publisher>eSecure</ns2:Publisher>
<ns2:SmallImage>
<ns2:URL>http://ecx.images-amazon.com/images/I/41OIjmpza2L._SL75_.jpg</ns2:URL>
<ns2:Height Units="pixels">75</ns2:Height>
<ns2:Width Units="pixels">75</ns2:Width>
</ns2:SmallImage>
<ns2:Studio>eSecure</ns2:Studio>
<ns2:Title>eSecure - Extra Comfort Bike Bicycle Gel Saddle Seat Cover</ns2:Title>
</ns2:ItemAttributes>
</AttributeSets>
<Relationships/>
<SalesRankings>
<SalesRank>
<ProductCategoryId>sports_display_on_website</ProductCategoryId>
<Rank>398</Rank>
</SalesRank>
<SalesRank>
<ProductCategoryId>458338031</ProductCategoryId>
<Rank>1</Rank>
</SalesRank>
</SalesRankings>
</Product>
</GetMatchingProductResult>
<ResponseMetadata>
<RequestId>9b44aba6-d1fa-486a-8114-2bc4f9311d8d</RequestId>
</ResponseMetadata>
</GetMatchingProductResponse>
答案 0 :(得分:2)
我知道这是一个古老的问题但我在同样的事情上遇到了困难。
首先,simplexml_load_string不处理这样抛出的命名空间,所以你可以递归地检查然后加载命名空间,评估xml并浪费很多时间......
在我的情况下,我不关心用命名空间来评估xml的格式,所以我只是用regex从字符串中删除它们。在此之后,simplexml_load_string将不再省略这些部分,并且使用json_encode和json_decode很容易从中获取数组。
//convert an xml string into an array
function xml2array($xml){
$xml = preg_replace('/(<\/?)\w+:([^>]*>)/', '$1$2', $xml); //get rid of namespaces
$xml = simplexml_load_string($xml);
return json_decode(json_encode($xml),true); //use built-in stuff
}
唯一的问题是当xml充满包含有用数据的属性时,你需要其他东西而不是simplexml_load_string。请注意,它返回一个@attributes元素:
<GetMatchingProductResult ASIN="B003IOSNNQ" status="Success">
但是从这部分中省略了它:
<Width Units="pixels">75</Width>
我不知道它为什么或如何深入检查嵌套标签。
答案 1 :(得分:0)
echo $ xmlobj-&gt; children('',true) - &gt; ListMatchingProductsResult-&gt; Products-&gt; Product-&gt; AttributeSets-&gt; children('ns2',true) - &gt; ItemAttributes-&gt; Brand.PHP_EOL;