php - 解析 Amazon MWS Scratchpad 响应

标签 php xml-parsing amazon-web-services

我正在尝试解析来自亚马逊的 xml 文件,但发现困难。我正在使用 simplexml_load_string

$xml = simplexml_load_string( 'My xml here' );

但是当我这样做的时候

echo $xml->GetMatchingProductResult->Product->AttributeSets;

什么也没显示

如何获取品牌值(value)?

我的 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>

最佳答案

我知道这是一个老问题,但我很难解决同样的问题。

首先 simplexml_load_string 不处理像这样抛出的 namespace ,因此您可以递归地检查然后加载 namespace ,评估 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。请注意,它从中返回一个 @attributes 元素:

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

但在这部分中省略了它:

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

我不知道为什么它会检查嵌套标签,也不知道它会检查嵌套标签的深度。

关于php - 解析 Amazon MWS Scratchpad 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15553519/

相关文章:

amazon-web-services - 无法删除 AWS Lambda 函数

r - 在 R 中使用 Segue 无限期配置 EMR 集群

php - in_array 函数未给出预期结果

PHP/MySQL - 调用成员函数 query()

java - 从 XML 中多次提取数据的最佳且优化的方法是什么?

java - SAXParser 和 XMLReader 之间的区别

php - 使用不同的 HTTP 方法重定向

php - Elasticsearch 评分始终为 0

javascript - 使用 R 从 xml 节点提取 javascript 语法

amazon-web-services - AWS Fn::ImportValue !Sub 不接受值