0

这是我的亚马逊 mws api 响应

<?xml version="1.0"?>
<GetMyPriceForSKUResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetMyPriceForSKUResult SellerSKU="ds-tru-6sss" 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>Assssssss</MarketplaceId>
        <ASIN>sss</ASIN>
      </MarketplaceASIN>
      <SKUIdentifier>
        <MarketplaceId>Afasrfd</MarketplaceId>
        <SellerId>ssssss</SellerId>
        <SellerSKU>dssss</SellerSKU>
      </SKUIdentifier>
    </Identifiers>
    <Offers>
      <Offer>
        <BuyingPrice>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>12.49</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>12.49</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </BuyingPrice>
        <RegularPrice>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>12.49</Amount>
        </RegularPrice>
        <FulfillmentChannel>MERCHANT</FulfillmentChannel>
        <ItemCondition>New</ItemCondition>
        <ItemSubCondition>New</ItemSubCondition>
        <SellerId>Aadada</SellerId>
        <SellerSKU>ssss</SellerSKU>
      </Offer>
      <Offer>
        <BuyingPrice>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>1000.00</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>1000.00</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </BuyingPrice>
        <RegularPrice>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>1000.00</Amount>
        </RegularPrice>
        <FulfillmentChannel>MERCHANT</FulfillmentChannel>
        <ItemCondition>New</ItemCondition>
        <ItemSubCondition>New</ItemSubCondition>
        <SellerId>ssss</SellerId>
        <SellerSKU>sss</SellerSKU>
      </Offer>
    </Offers>
  </Product>
</GetMyPriceForSKUResult>
<ResponseMetadata>
  <RequestId>e0ef1c2c-4f35-4316-8629-faadadd</RequestId>
</ResponseMetadata>
</GetMyPriceForSKUResponse>

amount (12.49)并从中选择

<ListingPrice>
    <CurrencyCode>USD</CurrencyCode>
    <Amount>12.49</Amount>
</ListingPrice>

我在尝试 ,

// from curl
$result = curl_exec ($ch);
$xmldoc = new DOMDocument();
$xmldoc->load($result);
$xpathvar = new Domxpath($xmldoc);

$queryResult = $xpathvar->query('/Amount');
foreach($queryResult as $result){
    echo $result;
}

我期待超过一个价值,但我根本没有得到。

抱歉,我不擅长 XPath,有人可以指导我吗?

4

3 回答 3

2

目前我在您的代码中发现了错误:


第一:使用两个//来选择一个元素,无论它位于 xml 树中的什么位置。

 $queryResult = $xpathvar->query('//Amount');

第二:感谢@Ranon。您将处理文档 xml 命名空间:

// Register Namespace mws
$xpathvar->registerNamespace("mws", 'http://mws.amazonservices.com/schema/Products/2011-10-01');

...并使用它,意味着:

 $queryResult = $xpathvar->query('//mws:Amount');

第三:如果要选择文本节点(<amount>节点之间),您应该使用:

$queryResult = $xpathvar->query('//mws:Amount/text()');

否则,您可以选择父元素<Amount>(就像您已经做的那样)并使用 PHP 检索值。然后,您必须将代码更改为:

$queryResult = $xpathvar->query('//mws:Amount');
foreach($queryResult as $result){
   echo $result->nodeValue; // echo the node value, not the node 'itself'
}

第四:还要注意代码中的另一个错误。当您从 xml 字符串创建 DOMDocument 时,您必须使用:

$document->loadXML($result);

第五:您告诉您要检索<Amount>元素内部的<ListingPrice>元素。<Amount>请注意,元素中也有<RegularPrice>元素。因此,元素在树中的位置确实很重要。<Amount>使用以下查询仅获取标价金额:

$queryResult = $xpathvar->query('//mws:ListingPrice/mws:Amount');
于 2013-01-30T22:05:20.727 回答
2

Amazon 使用您必须声明和使用的命名空间返回 XML。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// from curl
$result = curl_exec($ch);
$xmldoc = new DOMDocument();
$xmldoc->loadXML($result);
$xpathvar = new Domxpath($xmldoc);
// Register Namespace mws
$xpathvar->registerNamespace("mws", 'http://mws.amazonservices.com/schema/Products/2011-10-01');

// Query using namespace mws
$queryResult = $xpathvar->query('//mws:Amount');
foreach($queryResult as $result){
    echo $result->nodeValue;
}

我从子域中任意选择了命名空间标识符mws,您可以根据需要选择另一个。

我更正了@hek2mgl 发现的代码中的一些其他错误。

于 2013-01-30T22:27:05.310 回答
0

XPath 表达式错误。您需要'//Amount'选择所有“金额”元素

于 2013-01-30T22:05:12.723 回答