Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Created April 22, 2017 19:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save douglascayers/13903fae563b10457516b600b3ab9e3e to your computer and use it in GitHub Desktop.
Save douglascayers/13903fae563b10457516b600b3ab9e3e to your computer and use it in GitHub Desktop.
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>' +
'</root>';
// 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( myMatcher.group(), myMatcher.group(2).escapeXML() );
}
// 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>
@douglascayers
Copy link
Author

@akouayri
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