0

如何检索<ASIN>..</ASIN>此 xml 的所有值并将它们保存到 php 变量/数组?

我试过$result=$xml->xpath('//ASIN');了,但这给了我array(0) { }一个浏览器输出。谢谢您的帮助!我非常感激!

这是我的 xml 文件:

    <?xml version="1.0" ?>
      <ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
      <OperationRequest>
        <RequestId>...</RequestId>
      <Arguments>
       <Argument Name="region" Value="de"></Argument>
       <Argument Name="ItemId" Value="B007ZXLRVQ"></Argument>
       <Argument Name="Operation" Value="ItemLookup"></Argument>
       <Argument Name="Service" Value="AWSECommerceService"></Argument>
       <Argument Name="AWSAccessKeyId" Value="...."></Argument>
       <Argument Name="Timestamp" Value="2012-11-27T12:52:41Z"></Argument>
       <Argument Name="Signature" Value="...."></Argument>
       <Argument Name="ResponseGroup" Value="Similarities"></Argument>
       <Argument Name="AssociateTag" Value="..."></Argument>
       <Argument Name="Version" Value="2009-10-01"></Argument>
     </Arguments>
    <RequestProcessingTime>0.0216670000000000</RequestProcessingTime>
    </OperationRequest>
    <Items>
       <Request>
         <IsValid>True</IsValid>
         <ItemLookupRequest>
           <IdType>ASIN</IdType>
           <ItemId>B007ZXLRVQ</ItemId>
           <ResponseGroup>Similarities</ResponseGroup>
           <VariationPage>All</VariationPage>
         </ItemLookupRequest>
      </Request>
     <Item>
      <ASIN>B007ZXLRVQ</ASIN>
      <SimilarProducts><SimilarProduct><ASIN>B007Y6DS84</ASIN>
       <Title>Brita 040 147 Starterpaket Marella Cool, weiß</Title>
      </SimilarProduct>
      <SimilarProduct>
       <ASIN>B0018K7EMI</ASIN>
       <Title>Trinkflasche 1,0 Liter für gefiltertes Wasser</Title>
      </SimilarProduct>
      <SimilarProduct>
       <ASIN>B000QG7AJM</ASIN>
       <Title>Brita Filterkartuschen Maxtra Pack 4 (lim. Edition)</Title>
      </SimilarProduct>
      <SimilarProduct>
       <ASIN>B001T9N51M</ASIN>
       <Title>Brita Filterkartuschen Maxtra Pack 12 (lim. Edition)</Title>
      </SimilarProduct>
      <SimilarProduct>
       <ASIN>B001ET6CNU</ASIN>
       <Title>wuta kamille 82158 Herbacin Handcreme + Glyzerin Dose 75 ml, Fünferpack</Title>
     </SimilarProduct>
    </SimilarProducts>
   </Item>
  </Items>
 </ItemLookupResponse>
4

1 回答 1

0

由于命名空间,您无法直接执行 XPath 查询。在此处阅读完整的详细信息解决方案

我仍然在这里提供一些可以帮助你的代码。

$asins = simplexml_load_file('asin.xml');
if(count($asins)):
    //alternate way other than registring NameSpace
    //$asin = $asins->xpath("//*[local-name() = 'ASIN']");

    $asins->registerXPathNamespace('prefix', 'http://webservices.amazon.com/AWSECommerceService/2011-08-01');
    $asin = $asins->xpath("//prefix:ASIN");
    echo "<pre>";print_r($asin);
endif;

现在您将看到数组关闭ASIN元素,如下所示

Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => B007ZXLRVQ
        )

    [1] => SimpleXMLElement Object
        (
            [0] => B007Y6DS84
        )

    [2] => SimpleXMLElement Object
        (
            [0] => B0018K7EMI
        )

    [3] => SimpleXMLElement Object
        (
            [0] => B000QG7AJM
        )

    [4] => SimpleXMLElement Object
        (
            [0] => B001T9N51M
        )

    [5] => SimpleXMLElement Object
        (
            [0] => B001ET6CNU
        )

)
于 2013-04-30T11:01:08.843 回答