After hours of searching and using complicated functions to parse XML in PHP (simplexml) with CDATA I found this link that gives you the solution simply.
Normal XML files when passed to simplexml_load_file() will return an array with the tag name and value like this:
XML:
<GradeLevel> <GradeLevelItem> <GradeID type='Number'>1</GradeID> <Grade type='Text'>Primary K-3</Grade> </GradeLevelItem> <GradeLevel>
SimpleXML output
SimpleXMLElement Object
(
[GradeID] => 1
[Grade] => Primary K-3
)
But, when the value is in CDATA you will not be able to access the value directly, like this:
XML:
<GradeLevel> <GradeLevelItem> <GradeID type='Number'><![CDATA[1]]></GradeID> <Grade type='Text'><![CDATA[Primary K-3]]></Grade> </GradeLevelItem> <GradeLevel>
SimpleXML output
SimpleXMLElement Object
(
[GradeID] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => Number
)
)
[Grade] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => Text
)
)
)
To get the outut in the exact same way as the first case (to access the value) all you need to do is to pass a few extra params and the built in simplexml_load_file() function will do it for you automatically like this:
simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA);
This is the link that saved me:
http://blog.evandavey.com/2008/04/how-to-fix-simplexml-cdata-problem-in-php.html
