Apex code snippet demonstrating how to parse CDATA in XML using Dom.XMLNode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
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
As answered on https://salesforce.stackexchange.com/a/171088/987