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

使用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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同