gpt4 book ai didi

php - 使用 XPath 选择/过滤 XML 元素

转载 作者:行者123 更新时间:2023-12-04 16:58:59 24 4
gpt4 key购买 nike

这是我的亚马逊 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;
}

我期望为此提供更多的值(value),但我根本没有得到任何值(value)。

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

最佳答案

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

第一:用两个//选择一个元素,而不管它在 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');

关于php - 使用 XPath 选择/过滤 XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14614671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com