2013-03-21 112 views
2

我想從亞馬遜解析xml文件,但發現困難。我使用simplexml_load_string解析亞馬遜MWS Scratchpad響應

$ XML = simplexml_load_string( '我在這裏的XML');

但是當我做

回聲$ XML-> GetMatchingProductResult->產品 - > 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> 

回答

2

我知道這是一個老問題,但我有一個很難用同樣的事情。

首先simplexml_load_string不處理拋出這樣的命名空間,所以你可以遞歸地去看看,然後加載的命名空間,評估XML,浪費了大量的時間...

在我來說,我不關心用命名空間來評估xml的格式,所以我只是用正則表達式將它們從字符串中移除。在這個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。請注意,從這個返回@屬性元素:

<GetMatchingProductResult ASIN="B003IOSNNQ" status="Success"> 

但從這部分省略它:

<Width Units="pixels">75</Width> 

我不知道爲什麼或如何在內心深處,將在嵌套標籤檢查。

+0

你救了我幾個小時的挫折,非常感謝。 – 2015-11-06 13:33:36

0

echo $ xmlobj-> children('',true) - > ListMatchingProductsResult-> Products-> Product-> AttributeSets-> children('ns2',true) - > ItemAttributes-> Brand.PHP_EOL;