0

I have the following xml file

<?xml version="1.0" encoding="UTF-8"?> 
<data>
   <item name="general.global.Event"><![CDATA[EVENT!]]></item>
   <item name="general.global.CompanyName"><![CDATA[some name]]></item>
   <item name="general.global.CompanyImprint"><![CDATA[Legal information]]></item>
</data>

and my code is as follows

$xml = simplexml_load_file("general.xml") or die("Error: Cannot create object");
print_r($xml);

and my output is missing the CDATA.. how?

SimpleXMLElement Object
(
[item] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [name] => general.global.Event
                    )

            )

        [1] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [name] => general.global.CompanyName
                    )

            )

        [2] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [name] => general.global.CompanyImprint
                    )

            )

    )

)

Anand
  • 4,182
  • 6
  • 42
  • 54

2 Answers2

3

Text nodes are not exposed with print_r.

You can see the data there is you look at it explicitly:

print $xml->item[0];
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

The CDATA is being read, read this answer and you'll see that if you print_r($xml->asXML()); The parser recompiles the CDATA information just fine.

For some reason, PHP's var_dump and print_r don't have accurate representation of XML objects. Try this and you can still access the data:


foreach ($xml->item as $item) {
    if ('general.global.CompanyImprint' === (string)$item['name']) {
        var_dump((string)$item);
    }
}

// prints

string(17) "Legal information"
Prof
  • 2,898
  • 1
  • 21
  • 38