首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在MWS GetMatchingProduct中解析关系?

如何在MWS GetMatchingProduct中解析关系?
EN

Stack Overflow用户
提问于 2016-09-20 00:29:51
回答 2查看 517关注 0票数 9

数据:

代码语言:javascript
运行
复制
<Relationships>
      <ns2:VariationChild>
        <Identifiers>
          <MarketplaceASIN>
            <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
            <ASIN>B002KT3XQC</ASIN>
          </MarketplaceASIN>
        </Identifiers>
        <ns2:Color>Black</ns2:Color>
        <ns2:Size>Small</ns2:Size>
      </ns2:VariationChild>
      <ns2:VariationChild>
        <Identifiers>
          <MarketplaceASIN>
            <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
            <ASIN>B002KT3XQW</ASIN>
          </MarketplaceASIN>
        </Identifiers>
        <ns2:Color>Black</ns2:Color>
        <ns2:Size>Medium</ns2:Size>
      </ns2:VariationChild>
</Relationships>

“守则”:

代码语言:javascript
运行
复制
$data = simplexml_load_string($response);
foreach($data->GetMatchingProductResult AS $GetMatchingProductResult){
     $Product = $GetMatchingProductResult->Product;
     $Relationships = $Product->Relationships;

     foreach($Relationships->children('ns2', true)->VariationChild AS $VariationChild){

          $Identifiers = $VariationChild->Identifiers;
               $MarketplaceASIN = $Identifiers->MarketplaceASIN;
                    $MarketplaceId = $MarketplaceASIN->MarketplaceId;
                    $ASIN = $MarketplaceASIN->ASIN;

                    echo "$ASIN<br />";

     }
}

这与返回结果相呼应,但没有数据,因此它实际上是在遍历XML。但是,我尝试的任何东西都不会返回$ASIN变量中的数据。这是因为名称空间或simpleXML,还是我完全遗漏了其他东西?

编辑:其他尝试过的方法

代码语言:javascript
运行
复制
foreach($Relationships->children('http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd', true)->VariationChild AS $VariationChild){

     $Identifiers           = $VariationChild->Identifiers;
          $MarketplaceASIN  = $Identifiers->MarketplaceASIN;
               $MarketplaceId   = $MarketplaceASIN->MarketplaceId;
               $ASIN            = $MarketplaceASIN->ASIN;

               echo "[$ASIN]<br />";

}

$test = new SimpleXMLElement($response);
$test->registerXPathNamespace('ns2', 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd');
$variations = $test->xpath('//ns2:VariationChild');

foreach($variations AS $vars){

     print_r($vars);

}

两个人似乎都没有循环数据。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-05 13:51:52

以下代码获取ASIN字符串:

代码语言:javascript
运行
复制
$data = simplexml_load_string($response);

foreach ($data->GetMatchingProductResult as $GetMatchingProductResult) {
  $Product = $GetMatchingProductResult->Product;
  $Relationships = $Product->Relationships;

  foreach ($Relationships->children('ns2', true)->VariationChild
    as $VariationChild)
  {
    foreach ($VariationChild->children('', true) as $var_child) {
      echo $var_child->MarketplaceASIN->ASIN, PHP_EOL;
    }
  }
}

值得一提的是,真实响应格式不同于您发布的内容。

票数 6
EN

Stack Overflow用户

发布于 2016-09-24 10:41:30

是的,它使用的名称空间不正确。将代码中的'ns2‘替换为完整的命名空间"http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd“。

一个更好的解决方案是使用registerXPathNamespace,然后使用xpath访问子元素。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39583983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档