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