4

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.

4
  • Do with loops. first loop first xml array make array with the choice id as index and name will be the second index and then loop second output add attribute in corresponding index .. Commented Aug 27, 2014 at 8:20
  • $xml->div->attributes() for the first one ? Use loop following your need. Commented Aug 27, 2014 at 8:22
  • Here are several JSON formats for XML serialization, JsonML, BadgerFish, Rayfish, ... What kind of target format do you want to create? Commented Aug 27, 2014 at 8:29
  • 1
    @Abhishek have you got a solution ? Commented Nov 18, 2019 at 9:27

1 Answer 1

4

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Deprecated PHP8 "each" can be replaced with: $childTagName = key($childArray); $childProperties = current($childArray);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.