AI有一个XML,它具有属性和值。我想将它与属性和值一起转换为数组或数组对象。
XML
<?xml version="1.0" encoding="UTF-8"?>
<itemBody>
<div label="options">
<optionchoices optionIdentifier="RESPONSE" shuffle="false" maxOptions="1">
<choice identifier="A1"><![CDATA[aaaa]]></choice>
<choice identifier="A2"><![CDATA[bbbb]]></choice>
<choice identifier="A3"><![CDATA[cccc]]></choice>
</optionchoices>
</div>
</itemBody>
我尝试了两组代码,但结果并不像预期的那样。
代码1
<?php
$xml = simplexml_load_file('test.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
输出
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[@attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[@attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => aaaa
[1] => bbbb
[2] => cccc
)
)
)
)
在上面的输出中,如果我们在选择节点中检查,我们只得到值而不是属性
代码2
<?php
$xml = simplexml_load_file('test.xml');
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
输出
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[@attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[@attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[identifier] => A1
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[identifier] => A2
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[identifier] => A3
)
)
)
)
)
)
在此输出中,我们只获得XML的属性。
现在我想要的是获取属性以及XML的值。
请帮忙。
提前致谢。