1

I am trying to parse an XML response from the Amazon API in PHP via simplexml_load_string().

The XML i get looks like that:

<?xml version="1.0" encoding="UTF-8"?>
<GetMatchingProductForIdResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
   <GetMatchingProductForIdResult Id="xxx" IdType="SellerSKU" status="Success">
      <Products>
         <Product>
            <Identifiers>
               <MarketplaceASIN>
                  <MarketplaceId>xxxx</MarketplaceId>
                  <ASIN>xxx</ASIN>
               </MarketplaceASIN>
            </Identifiers>
            <AttributeSets>
               <ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="de-DE">
                  <ns2:Binding>Elektronik</ns2:Binding>
                  <ns2:Brand>Panasonic</ns2:Brand>
                  <ns2:Feature>xx</ns2:Feature>
                  <ns2:Feature>xxx</ns2:Feature>
                  <ns2:Feature>xxx</ns2:Feature>
                  <ns2:Feature>xxx</ns2:Feature>
                  <ns2:Feature>xxx</ns2:Feature>
                  <ns2:Label>Panasonic</ns2:Label>
                  <ns2:Manufacturer>Panasonic</ns2:Manufacturer>
                  <ns2:PackageDimensions>
                     <ns2:Height Units="inches">xx</ns2:Height>
                     <ns2:Length Units="inches">xx</ns2:Length>
                     <ns2:Width Units="inches">xxx</ns2:Width>
                  </ns2:PackageDimensions>
                  <ns2:PartNumber>xxx</ns2:PartNumber>
                  <ns2:ProductGroup>Computer &amp; Zubehör</ns2:ProductGroup>
                  <ns2:ProductTypeName>COMPUTER_COMPONENT</ns2:ProductTypeName>
                  <ns2:Publisher>Panasonic</ns2:Publisher>
                  <ns2:SmallImage>
                     <ns2:URL>xxx.jpg</ns2:URL>
                     <ns2:Height Units="pixels">xx</ns2:Height>
                     <ns2:Width Units="pixels">xx</ns2:Width>
                  </ns2:SmallImage>
                  <ns2:Studio>Panasonic</ns2:Studio>
                  <ns2:Title>xxx</ns2:Title>
               </ns2:ItemAttributes>
            </AttributeSets>
            <Relationships />
            <SalesRankings />
         </Product>
      </Products>
   </GetMatchingProductForIdResult>
   <ResponseMetadata>
      <RequestId>xx</RequestId>
   </ResponseMetadata>
</GetMatchingProductForIdResponse>    

My PHP code:

$response = $service->GetMatchingProductForId($request);  //making the call
$response = $response->toXML();
$response = simplexml_load_string($response);       
var_dump($response);     

Unfortunatly the AttributeSets don't get converted into objects as you can see in the var_dump.

object(SimpleXMLElement)#9 (2) {
  ["GetMatchingProductForIdResult"]=>
  object(SimpleXMLElement)#4 (2) {
    ["@attributes"]=>
    array(3) {
      ["Id"]=>
      string(5) "xxx"
      ["IdType"]=>
      string(9) "SellerSKU"
      ["status"]=>
      string(7) "Success"
    }
    ["Products"]=>
    object(SimpleXMLElement)#14 (1) {
      ["Product"]=>
      object(SimpleXMLElement)#18 (4) {
        ["Identifiers"]=>
        object(SimpleXMLElement)#23 (1) {
          ["MarketplaceASIN"]=>
          object(SimpleXMLElement)#29 (2) {
            ["MarketplaceId"]=>
            string(14) "xxx"
            ["ASIN"]=>
            string(10) "xxxx"
          }
        }
        ["AttributeSets"]=>
        object(SimpleXMLElement)#37 (0) {
        }
        ["Relationships"]=>
        object(SimpleXMLElement)#28 (0) {
        }
        ["SalesRankings"]=>
        object(SimpleXMLElement)#26 (0) {
        }
      }
    }
  }
  ["ResponseMetadata"]=>
  object(SimpleXMLElement)#12 (1) {
    ["RequestId"]=>
    string(36) "xxx"
  }
}

Is something wrong with the XML?

user1369594
  • 183
  • 10

3 Answers3

6

Nuts to this, just used blunt force and it worked fine.

$response = str_replace("ns2:","",$response);
user1369594
  • 183
  • 10
0

Sometimes var_dump() doesn't show all the elements (mostly it has too much elements or too much indent). its only about view, but your objects will be there, try to dump only AttributeSets self

Santa's helper
  • 976
  • 8
  • 21
  • still only getting an empty object `object(SimpleXMLElement)#4 (0) { }` if i try to access an attribute it returns NULL – user1369594 Sep 02 '14 at 08:56
  • Can you please provide the php line of your AttributeSets var_dump. How do you reach that element? – Santa's helper Sep 02 '14 at 09:24
  • i think products is an array so you need to do loop or if its certain only one product you can use like: blabla->products[0]->AttributeSets – Santa's helper Sep 02 '14 at 09:28
  • `$response->GetMatchingProductForIdResult->Products->Product->AttributeSets` returns `object(SimpleXMLElement)#4 (0) { }` as mentioned above. Maybe it's because of the different Namespace? – user1369594 Sep 02 '14 at 09:55
  • 1
    i have just noticed that AttributeSets's elements has namespace prefix i am not so sure but it can couse your problem [check this issue](http://stackoverflow.com/questions/16412047/parse-xml-namespaces-with-php-simplexml) seems like xpath will be your solution – Santa's helper Sep 02 '14 at 09:58
0

You can call children on your SimpleXMLElement like

$xml = <<<EOF
<AttributeSets>
   <ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="de-DE">
      <ns2:Binding>Elektronik</ns2:Binding>
      <ns2:Brand>Panasonic</ns2:Brand>
      <ns2:Feature>xx</ns2:Feature>
      <ns2:Feature>xxx</ns2:Feature>
      <ns2:Feature>xxx</ns2:Feature>
      <ns2:Feature>xxx</ns2:Feature>
      <ns2:Feature>xxx</ns2:Feature>
      <ns2:Label>Panasonic</ns2:Label>
      <ns2:Manufacturer>Panasonic</ns2:Manufacturer>
      <ns2:PackageDimensions>
         <ns2:Height Units="inches">xx</ns2:Height>
         <ns2:Length Units="inches">xx</ns2:Length>
         <ns2:Width Units="inches">xxx</ns2:Width>
      </ns2:PackageDimensions>
      <ns2:PartNumber>xxx</ns2:PartNumber>
      <ns2:ProductGroup>Computer &amp; Zubehör</ns2:ProductGroup>
      <ns2:ProductTypeName>COMPUTER_COMPONENT</ns2:ProductTypeName>
      <ns2:Publisher>Panasonic</ns2:Publisher>
      <ns2:SmallImage>
         <ns2:URL>xxx.jpg</ns2:URL>
         <ns2:Height Units="pixels">xx</ns2:Height>
         <ns2:Width Units="pixels">xx</ns2:Width>
      </ns2:SmallImage>
      <ns2:Studio>Panasonic</ns2:Studio>
      <ns2:Title>xxx</ns2:Title>
   </ns2:ItemAttributes>
</AttributeSets>
EOF;

$sxe = new SimpleXMLElement($xml);
$ns2 = $sxe->children('ns2', TRUE);

var_dump(count($ns2));

// prints int(1)

In the php docs: http://php.net/manual/en/simplexmlelement.children.php

public SimpleXMLElement SimpleXMLElement::children ([ string $ns [, bool $is_prefix = false ]] )
thealexbaron
  • 1,558
  • 1
  • 11
  • 25