dry69034 2012-12-19 08:59
浏览 143
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • dongzj2015 2012-12-21 09:28
    关注

    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...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥60 有没有朋友懂那种地图软件逻辑的?
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办