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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效