PHP: How to handle <![CDATA[ with SimpleXMLElement?

PHP: How to handle <![CDATA[ with SimpleXMLElement?

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP: How to handle <![CDATA[ with SimpleXMLElement?

When parsing XML documents that contain CDATA sections, SimpleXMLElement often returns NULL for those nodes. This isn't a bug in your logic; it is a deliberate design choice regarding how the library exposes node contents. By default, SimpleXMLElement treats the text inside a CDATA block as raw data rather than parsed content. When you attempt to access this value directly via string casting or standard property access, the internal representation doesn't align with the expectation of a simple string, resulting in a NULL return or an empty string depending on the PHP version and specific context.

Consider the following XML snippet:

<content><![CDATA[Hello, world!]]></content>

If you parse this using SimpleXMLElement, accessing $xml->content will yield NULL. This happens because the library distinguishes between "text content" and "CDATA content." To retrieve the actual string Hello, world!, you must explicitly cast the value to a string. The syntax is straightforward:

$xml = new SimpleXMLElement('<content><![CDATA[Hello, world!]]></content>');
$rawValue = (string) $xml->content; // Returns "Hello, world!"

The type cast forces PHP to retrieve the internal buffer associated with that node without parsing any nested child elements. Without this cast, SimpleXMLElement attempts to interpret the content, and since CDATA is meant to be opaque text, it fails to map it to a standard string property in older contexts or specific configurations, leading to the confusion of seeing NULL.

It is important to understand why this distinction exists. XML parsers generally separate CDATA from regular text because CDATA allows developers to include characters that would otherwise have special meaning in an XML document, such as <, >, and &. The parser stores these sequences exactly as they appear in the source file. When you access a standard element, PHP expects a parsed string. However, accessing a CDATA node requires acknowledging that the content is uninterpreted text.

If your XML structure is more complex, such as having multiple elements where some contain CDATA and others contain regular text, relying on implicit casting might lead to inconsistent behavior if not handled carefully. A robust approach involves checking the node type or explicitly casting every value you expect to be a string.

$xml = new SimpleXMLElement('<content><![CDATA[Hello, world!]]></content>');

// Explicit cast ensures we get the raw CDATA content
$cdataContent = (string) $xml->content; 

// If this were mixed with regular text:
// <mixed><text>Plain Text</text></mixed>
// <mixed><raw><![CDATA[<b>Bold</b>]]></raw></mixed>

One common pitfall occurs when developers assume that because an element exists, it must contain a string. If you access $xml->content without the cast in a context where the library expects to return SimpleXMLElement objects for nested structures (though CDATA usually prevents nesting), you might encounter unexpected types. However, in the specific case of pure CDATA sections, the cast is the primary solution.

For larger applications or when working with frameworks like Laravel, handling XML data often involves external libraries like DomDocument or strict type hinting strategies to avoid these edge cases. While SimpleXMLElement is convenient for quick parsing tasks, its handling of CDATA can be a source of subtle bugs if the developer overlooks the need for explicit string casting. If you are building a module that processes XML feeds frequently, consider whether DomDocument might offer more predictable behavior for mixed content types, as it exposes the DOM level directly without some of the abstraction layers that hide these nuances in SimpleXMLElement.

Ultimately, the fix is simple but specific: always cast CDATA nodes to strings when accessing their content. This small step ensures your application handles raw XML data correctly regardless of what characters are inside those sections.

Our network