php - 使用 XPath 提取 xml 数据

标签 php xml xpath namespaces simplexml

我试图从以下 xml 中提取数据,但没有运气。

<?xml version="1.0"?>
<GetMatchingProductResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetMatchingProductResult ASIN="1908256052" 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>1908256052</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <AttributeSets>
      <ns2:ItemAttributes xml:lang="en-GB">
        <ns2:Author>Slimming World</ns2:Author>
        <ns2:Binding>Hardcover</ns2:Binding>
        <ns2:Format>Illustrated</ns2:Format>
        <ns2:IsEligibleForTradeIn>true</ns2:IsEligibleForTradeIn>
        <ns2:Label>Slimming World</ns2:Label>
        <ns2:Languages>
          <ns2:Language>
            <ns2:Name>english</ns2:Name>
            <ns2:Type>Unknown</ns2:Type>
          </ns2:Language>
          <ns2:Language>
            <ns2:Name>english</ns2:Name>
            <ns2:Type>Original Language</ns2:Type>
          </ns2:Language>
          <ns2:Language>
            <ns2:Name>english</ns2:Name>
            <ns2:Type>Published</ns2:Type>
          </ns2:Language>
        </ns2:Languages>
        <ns2:ListPrice>
          <ns2:Amount>16.99</ns2:Amount>
          <ns2:CurrencyCode>GBP</ns2:CurrencyCode>
        </ns2:ListPrice>
        <ns2:Manufacturer>Slimming World</ns2:Manufacturer>
        <ns2:NumberOfItems>1</ns2:NumberOfItems>
        <ns2:NumberOfPages>224</ns2:NumberOfPages>
        <ns2:PackageDimensions>
          <ns2:Height Units="inches">0.87</ns2:Height>
          <ns2:Length Units="inches">9.69</ns2:Length>
          <ns2:Width Units="inches">7.64</ns2:Width>
          <ns2:Weight Units="pounds">1.85</ns2:Weight>
        </ns2:PackageDimensions>
        <ns2:ProductGroup>Book</ns2:ProductGroup>
        <ns2:ProductTypeName>ABIS_BOOK</ns2:ProductTypeName>
        <ns2:PublicationDate>2011-11-20</ns2:PublicationDate>
        <ns2:Publisher>Slimming World</ns2:Publisher>
        <ns2:SmallImage>
          <ns2:URL>http://ecx.images-amazon.com/images/I/61aM-pJlQtL._SL75_.jpg</ns2:URL>
          <ns2:Height Units="pixels">75</ns2:Height>
          <ns2:Width Units="pixels">64</ns2:Width>
        </ns2:SmallImage>
        <ns2:Studio>Slimming World</ns2:Studio>
        <ns2:Title>Slimming World Extra Easy All in One</ns2:Title>
      </ns2:ItemAttributes>
    </AttributeSets>
    <Relationships/>
    <SalesRankings>
      <SalesRank>
        <ProductCategoryId>book_display_on_website</ProductCategoryId>
        <Rank>3304</Rank>
      </SalesRank>
      <SalesRank>
        <ProductCategoryId>271146</ProductCategoryId>
        <Rank>11</Rank>
      </SalesRank>
      <SalesRank>
        <ProductCategoryId>1039764</ProductCategoryId>
        <Rank>25</Rank>
      </SalesRank>
      <SalesRank>
        <ProductCategoryId>270704</ProductCategoryId>
        <Rank>38</Rank>
      </SalesRank>
    </SalesRankings>
  </Product>
</GetMatchingProductResult>
<ResponseMetadata>
  <RequestId>8d4fde2b-d2bb-4c06-800d-68eec2effcf3</RequestId>
</ResponseMetadata>
</GetMatchingProductResponse>

我正在使用以下函数来返回和处理响应
public function getXML2($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    $response = curl_exec($ch);

    $xml = simplexml_load_string($response, NULL, NULL, "http://mws.amazonservices.com/schema/Products/2011-10-01");
    $xml->registerXPathNamespace('ns2', 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd');
    return $xml;

}

当我尝试遍历数据时,我无法返回任何内容
foreach($xml->xpath('//ns2:ItemAttributes') as $event) {
    $event->registerXPathNamespace('ns2', 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd');
    echo var_export($event->xpath('//ns2:author'));
}

我究竟做错了什么?

最佳答案

你试了吗DOMDocument ?

$dom = new DOMDocument;
$dom->loadXML($xml);

$xpath = new DOMXPath($dom);
$xpath->registerNamespace('ns2', 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd');

$el = $xpath->query('//ns2:ItemAttributes/ns2:Author');
var_dump($el->item(0)->nodeValue);

//string(14) "Slimming World"

关于php - 使用 XPath 提取 xml 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12755610/

相关文章:

java - 我可以将变量应用于 Selenium WebDriver 上基于文本的 xPath 搜索吗?

xml - 如何使用 R 的 XML 库进行 xpath 查询?

php - 如何计算数组中的运行总计。 PHP

php - 每 15 秒执行一次 cron

java - XML SAX 解析

c# - 我需要返回匹配变量值的数据

php - ffmpeg 编码 mpeg

php - 在 PHP 中过滤掉 MySQL 查询

xml - XSL : why doesn't output indent work?

java - href 链接的 XPATH 不起作用。获取无效选择器 : It should be an element