Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Apex code snippet demonstrating how to parse CDATA in XML using Dom.XMLNode
String xml =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<root>' +
' <SomeNode><![CDATA[<b>contains html tags</b>]]></SomeNode>' +
// replace CDATA sections with parseable tokens
xml = xml.replaceAll( '<!\\[CDATA\\[', 'XML_CDATA_START' ).replaceAll( ']]>', 'XML_CDATA_END' );
// we will build up a map of original text and replacement text
Map<String, String> replacementMap = new Map<String, String>();
// regular expression to match sections we want to replace
Pattern myPattern = Pattern.compile( '(XML_CDATA_START)(.*?)(XML_CDATA_END)' );
Matcher myMatcher = myPattern.matcher( xml );
while ( myMatcher.find() ) {
// the regex was too complicated for Matcher.replaceFirst(..)
// so have to do it manually so just put in this map the
// original text and the replacement text, we do replacing later
replacementMap.put(, );
// replace in the xml each CDATA section with the escaped XML of its inner content
for ( String key : replacementMap.keySet() ) {
xml = xml.replace( key, replacementMap.get( key ) );
// parse the xml like normal
Dom.Document doc = new Dom.Document();
doc.load( xml );
Dom.XMLNode rootNode = doc.getRootElement();
String text = rootNode.getChildElement( 'SomeNode', null ).getText();
System.debug( text ); // prints: <b>contains html tags</b>
Copy link

Copy link

akouayri commented Sep 15, 2021

I implemented your code but I still get no result return from the element nodes I have a bunch of inner XML tags inside element node - when I run debug on XML i can see that element node doesn't include ![CDATA - any idea what I could be missing here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment