【问题标题】:Amazon MWS Products API XML Parsing with PHP使用 PHP 进行亚马逊 MWS 产品 API XML 解析
【发布时间】:2012-12-06 14:05:47
【问题描述】:

我正在尝试使用 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 放入一个关联数组。

【问题讨论】:

  • "我无法解析 xml。"为什么?
  • 没有错误,但是 $xmlData 为空。可能是带有命名空间的东西

标签: php xml-parsing amazon-web-services


【解决方案1】:

解决办法是:

$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 构造函数崩溃的原因......

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      相关资源
      最近更新 更多