I was quite amazed today. When loading a SOAP response into a SimpleXMLElement i noticed some fields were left blank. I should have checked the SOAP response first. But instead told our Delphi guy that the response was not filled correctly. This was not the case :)
When we both saw the SOAP response was in perfect shape. We started to poke around on the PHP side. The strange thing was all tag names were taken from the response correctly. It was just the data that was missing. Then we noticed the data missing was inside CDATA tags.
From a first glance at the PHP manual it it wasn’t clear what was going on. So i did some googleing. And found a good post by David Coallier. This post solved the problem. The example showed how to add an extra LIBXML options to the simplexml_load_String method. Although David provided the solution. I still wanted to make post. Maybe it will help somebody.
// parsing with CDATA tags using the *_load_string method
$xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);
// parsing with CDATA tags using the OO way
$xml = new SimpleXMLElement($string, LIBXML_NOCDATA);
The LIBXML options that can be passed to the *_load methods and constructor can be found in the php documentation.
It’s pretty damn weird though. I want to parse the CDATA tags inside my XML. And can only do so by providing the NOCDATA option.



Thijs Lensselink is a PHP developer, consultant and all out open source enthusiast.
He has over 12+ years of experience in building and maintaining web applications mostly
on linux/Unix/BSD platforms. Besides a full time job he does freelance work with his ...
Is there anything similar to solve this problem with Java Code?
Ralph Steins
19 Nov 08 at 08:27
Hello Ralph,
I’m not much of a Java guy. So i forwarded the question to a friend of mine. He didn’t answer yet however.
As far as i can see Java has a special interface for CDATA
public interface CDATASection extends Text {
}
It should be possible to just cast the CDATANode to a textNode.
CDATASection cdata = (CDATASection) current;
String data = cdata.getData();
Thijs Lensselink
19 Nov 08 at 12:12
You can either normalize the node(s) usign the normalize() function on the org.w3c.node object.
Or you can check if it is a CDATA object using the constant CDATA_SECTION_NODE.
Bart Stavenuiter
20 Nov 08 at 03:31
thank you so much! I was having a hard time trying to get text inside CDATA…
markushi
26 Feb 11 at 01:17