Sebastian Viereck
Sebastian Viereck

Reputation: 5923

Amazon MWS Products API XML Parsing with PHP

I am trying to parse the XML Part of the response with Simplexml without losing the "role" informations like "Komponist" or "Künstler" .

<itemattributes xml:lang="de-DE" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<ns2:binding>Audio CD</ns2:binding>
<ns2:brand>MEYER,SABINE/VARIOUS</ns2:brand>
<ns2:creator role="Künstler">Meyer,Sabine</ns2:creator>
<ns2:creator role="Künstler">Various</ns2:creator>
<ns2:creator role="Komponist">Mozart</ns2:creator>
<ns2:creator role="Komponist">Stamitz</ns2:creator>
<ns2:creator role="Komponist">Weber</ns2:creator>
<ns2:creator role="Komponist">Krommer</ns2:creator>
</ns2:itemattributes>

I have tried this:

    $nodeList = $attributeSets->getAny();
    foreach ($nodeList as $domNode){
        $domDocument =  new DOMDocument();
        $domDocument->preserveWhiteSpace = false;
        $domDocument->formatOutput = true;
        $xmlData = $domDocument->saveXML($domDocument->importNode($domNode,true));

    }
    //$xmlData  = str_replace("ns2:", "", $xmlData);
    $xmlData = new SimpleXMLElement($xmlData);

But if I do not replace the ns2 attributes, I can not parse the xml. And with uncommenting the line the role attributes are gone:

SimpleXMLElement Object
(
    [Binding] => Audio CD
    [Brand] => MEYER,SABINE/VARIOUS
    [Creator] => Array
    (
        [0] => Meyer,Sabine
        [1] => Various
        [2] => Mozart
        [3] => Stamitz
        [4] => Weber
        [5] => Krommer
    )
)

I would like to know, how I can hold these Attributes and maybe in the end how I could get the whole XML to an associative array.

Upvotes: 3

Views: 3215

Answers (2)

Sebastian Viereck
Sebastian Viereck

Reputation: 5923

The solution was:

$attributeSets = $product->getAttributeSets();
if ($attributeSets->isSetAny()){
$nodeList = $attributeSets->getAny();
$xmlData =  getXMLData($nodeList);
foreach ($xmlData as $node) {
         foreach($node->attributes() as $name => $value) {
             if($node->getName() == "Creator")
             {
                 $array['Creator'][] = array(
                     "name" => $node,
                     "role" => $value
                 );
             }
         }
     }

}

function getXMLData($nodeList)
{
    foreach ($nodeList as $domNode){
        $domDocument = new DOMDocument;
        $domDocument->preserveWhiteSpace = false;
        $domDocument->formatOutput = true;
        $xml = $domDocument->saveXML($domDocument->importNode($domNode,true));
    }
    return new SimpleXMLElement($xml, false, false, 'ns2', true);
}

Also very Important is the encoding, I had problems with iso encoding and the umlauts, thats why the SimpleXMLElement Constructor crashed...

Upvotes: 2

Gordon
Gordon

Reputation: 317197

Make sure you fix the first line to include the namespace:

$xml = <<< XML
<ns2:itemattributes xml:lang="de-DE" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
 <ns2:binding>Audio CD</ns2:binding>
 <ns2:brand>MEYER,SABINE/VARIOUS</ns2:brand>
 <ns2:creator role="Künstler">Meyer,Sabine</ns2:creator>
 <ns2:creator role="Künstler">Various</ns2:creator>
 <ns2:creator role="Komponist">Mozart</ns2:creator>
 <ns2:creator role="Komponist">Stamitz</ns2:creator>
 <ns2:creator role="Komponist">Weber</ns2:creator>
 <ns2:creator role="Komponist">Krommer</ns2:creator>
</ns2:itemattributes>
XML;

Then do the following to get the role attributes:

With DOM

$dom = new DOMDocument;
$dom->loadXML($xml);
foreach ($dom->getElementsByTagName('creator') as $creator) {
    printf(
        'Role: %s - Value: %s%s',
        $creator->getAttribute('role'),
        $creator->nodeValue,
        PHP_EOL
    );
}

With SimpleXml

$itemAttributes = new SimpleXMLElement($xml, null, false, 'ns2', true);
foreach ($itemAttributes->creator as $creator) {
    $attributes = $creator->attributes();
    printf(
        'Role: %s - Value: %s%s',
        $attributes['role'],
        $creator,
        PHP_EOL
    );
}

Upvotes: 1

Related Questions