繁体   English   中英

使用PHP的亚马逊MWS产品API XML解析

[英]Amazon MWS Products API XML Parsing with PHP

我试图用Simplexml解析响应的XML部分,而不会丢失像“Komponist”或“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>

我试过这个:

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

但是,如果我不替换ns2属性,我无法解析xml。 通过取消注释该行,角色属性消失了:

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

我想知道,我如何能够保存这些属性,最后我怎么能将整个XML转换为关联数组。

解决方案是:

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

同样非常重要的是编码,我遇到了iso编码和变音符号的问题,这就是SimpleXMLElement Constructor崩溃的原因......

确保修复第一行以包含命名空间:

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

然后执行以下操作以获取角色属性:

使用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
    );
}

使用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
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM