2

I am trying to parse xml file from amazon but found difficulties. I am using simplexml_load_string

$xml = simplexml_load_string( 'My xml here' );

but when I do

echo $xml->GetMatchingProductResult->Product->AttributeSets;

it shows nothing

How can I access the Brand value?

My xml file:

<?xml version="1.0"?>
<GetMatchingProductResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetMatchingProductResult ASIN="B003IOSNNQ" 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>B003IOSNNQ</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <AttributeSets>
      <ns2:ItemAttributes xml:lang="en-GB">
        <ns2:Binding>Misc.</ns2:Binding>
        <ns2:Brand>eSecure</ns2:Brand>
        <ns2:Feature>Colour: Classic Black</ns2:Feature>
        <ns2:Feature>Thickness: Approximately 40mm - More than msot sellers</ns2:Feature>
        <ns2:Feature>Dimensions [length x width at widest and narrowest]: approximately 280 x 190 x 70mm</ns2:Feature>
        <ns2:Feature>Material: Stretch Lycra, Silica Gel</ns2:Feature>
        <ns2:Feature>Ideal for Standard Bicycle / Static Exercise Bike</ns2:Feature>
        <ns2:Label>eSecure</ns2:Label>
        <ns2:Manufacturer>eSecure</ns2:Manufacturer>
        <ns2:PackageDimensions>
          <ns2:Height Units="inches">2.13</ns2:Height>
          <ns2:Length Units="inches">10.71</ns2:Length>
          <ns2:Width Units="inches">7.72</ns2:Width>
          <ns2:Weight Units="pounds">0.49</ns2:Weight>
        </ns2:PackageDimensions>
        <ns2:PackageQuantity>1</ns2:PackageQuantity>
        <ns2:ProductGroup>Sports</ns2:ProductGroup>
        <ns2:ProductTypeName>SPORTING_GOODS</ns2:ProductTypeName>
        <ns2:Publisher>eSecure</ns2:Publisher>
        <ns2:SmallImage>
          <ns2:URL>http://ecx.images-amazon.com/images/I/41OIjmpza2L._SL75_.jpg</ns2:URL>
          <ns2:Height Units="pixels">75</ns2:Height>
          <ns2:Width Units="pixels">75</ns2:Width>
        </ns2:SmallImage>
        <ns2:Studio>eSecure</ns2:Studio>
        <ns2:Title>eSecure - Extra Comfort Bike Bicycle Gel Saddle Seat Cover</ns2:Title>
      </ns2:ItemAttributes>
    </AttributeSets>
    <Relationships/>
    <SalesRankings>
      <SalesRank>
        <ProductCategoryId>sports_display_on_website</ProductCategoryId>
        <Rank>398</Rank>
      </SalesRank>
      <SalesRank>
        <ProductCategoryId>458338031</ProductCategoryId>
        <Rank>1</Rank>
      </SalesRank>
    </SalesRankings>
  </Product>
</GetMatchingProductResult>
<ResponseMetadata>
  <RequestId>9b44aba6-d1fa-486a-8114-2bc4f9311d8d</RequestId>
</ResponseMetadata>
</GetMatchingProductResponse>
debianek
  • 589
  • 1
  • 9
  • 30

2 Answers2

2

I know this is an old question but I had a hard time with the same thing.

First simplexml_load_string doesn't handle the namespaces thrown in like this, so you could go recursively and check and then load the namespaces, evaluate the xml and waste a lot of time ...

In my case I didn't care about evaluating the format of the xml with the namespaces, so I just removed them from the string with regex. After this simplexml_load_string won't omit those parts anymore and it's easy to get an array out of it with json_encode and json_decode.

//convert an xml string into an array
function xml2array($xml){
    $xml = preg_replace('/(<\/?)\w+:([^>]*>)/', '$1$2', $xml); //get rid of namespaces
    $xml = simplexml_load_string($xml);
    return json_decode(json_encode($xml),true); //use built-in stuff
}

The only problem with this is when the xml is full of attributes containing useful data, then you would need something else instead of simplexml_load_string. Note that it returns an @attributes element from this:

<GetMatchingProductResult ASIN="B003IOSNNQ" status="Success">

but omits it from this part:

<Width Units="pixels">75</Width>

I have no idea why or how deep down it would check in the nested tags.

Peter Rakmanyi
  • 1,475
  • 11
  • 13
0

echo $xmlobj->children('',true)->ListMatchingProductsResult->Products->Product->AttributeSets->children('ns2',true)->ItemAttributes->Brand.PHP_EOL;