doufan9805 2013-01-30 22:01
浏览 68
已采纳

使用XPath选择/过滤XML元素

here is my amazon mws api responce

<?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>

and to select amount (12.49) from

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

I am trying ,

// 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;
}

I am expecting more then one value for this, but I am getting none at all.

Sorry, I am not good at XPath, can somebody guide me?

  • 写回答

3条回答

  • dpd46554 2013-01-30 22:05
    关注

    Currently I found errors in your code:


    First: Use two // to select an element regardless of where it is located in the xml tree.

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

    Second: thanks @Ranon. You'll take care of the documents xml namespace:

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

    ... and use it, means:

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

    Third: If you want to select the text node (between the <amount> nodes) you should use:

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

    Otherwise you can select the parent element <Amount> (as you already doing) and retrieve the value with PHP. Then you have to change your code to:

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

    Fourth: Also note another error in your code. When you create a DOMDocument from an xml string you'll have to use:

    $document->loadXML($result);
    

    Fifth: You told that you want to retrieve the <Amount> elements form inside <ListingPrice> elements. Note that there are also <Amount> elements inside <RegularPrice> elements. So it does matter where the <Amount> element is located in tree. Use the following query to obtain only listing price amounts:

    $queryResult = $xpathvar->query('//mws:ListingPrice/mws:Amount');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题