0

How can I retrieve all <ASIN>..</ASIN> values of this xml and save them to a php variable/array?

I tried $result=$xml->xpath('//ASIN'); but this gives me array(0) { } as a browser output. Thank you for your help! I appreciate it very much!

This is my xml file:

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

1 Answer 1

1

Because of Namespace you're not able to execute XPath query directly. Read the full details solution here

Still I am providing here little bit code which can help you.

$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;

Now you will see array off ASIN element like below

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
        )

)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.