3

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

4

2 回答 2

2

解决方案是:

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

于 2012-12-21T09:28:18.740 回答
1

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

$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
    );
}
于 2012-12-19T09:28:31.433 回答