PHP XML to Array/Object with Attributes and Values

六月ゝ 毕业季﹏ 提交于 2020-01-04 02:38:23

问题


AI have an XML which have attributes as well as values in them. I want to convert it to an Array or Array Object along with attributes and values.

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>

I tried two set of code but the result was not as expected.

Code 1

<?php
$xml = simplexml_load_file('test.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>

Output

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
                        )

                )

        )

)

In the above output if we check then in choice node we get the values only and not the attributes

Code 2

<?php
$xml = simplexml_load_file('test.xml');
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>

Output

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
                                        )

                                )

                        )

                )

        )

)

In this output we get only attributes of XML.

Now what I want is to obtain Attributes as well as Values of the XML.

Please help.

Thanks in advance.


回答1:


This is what I got. And this is the solution which I expected.

http://outlandish.com/blog/xml-to-json/



来源:https://stackoverflow.com/questions/25522047/php-xml-to-array-object-with-attributes-and-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!