Skip to content

Instantly share code, notes, and snippets.

@richardvanhook
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardvanhook/8b29c27b66adb3fb65f5 to your computer and use it in GitHub Desktop.
Save richardvanhook/8b29c27b66adb3fb65f5 to your computer and use it in GitHub Desktop.
final String PAYLOAD_ORIGINAL = 'abc<123>def';
final String PAYLOAD_ESCAPED = PAYLOAD_ORIGINAL.escapeXml();
final String XML = '<foo><bar>' + PAYLOAD_ESCAPED + '</bar></foo>';
//READER
String PAYLOAD_FROM_READER = '';
final XmlStreamReader reader = new XmlStreamReader(XML);
for(Integer i=0; i<2; i++) reader.next();
for(Integer i=0; i<5; i++){
reader.next();
PAYLOAD_FROM_READER += reader.getText();
}
//DOM
final Dom.Document doc = new Dom.Document();
doc.load(XML);
final String PAYLOAD_FROM_DOM = doc.getRootElement().getChildElement('bar',null).getText();
System.assertEquals(PAYLOAD_ORIGINAL,PAYLOAD_FROM_DOM);
System.debug(
'\nPAYLOAD_ORIGINAL : [' + PAYLOAD_ORIGINAL + ']' +
'\nPAYLOAD_ESCAPED : [' + PAYLOAD_ESCAPED + ']' +
'\nXML : [' + XML + ']' +
'\nPAYLOAD_FROM_DOM : [' + PAYLOAD_FROM_DOM + ']' +
'\nPAYLOAD_FROM_READER: [' + PAYLOAD_FROM_READER + ']'
);
// Output will be:
// PAYLOAD_ORIGINAL : [abc<123>def]
// PAYLOAD_ESCAPED : [abc&lt;123&gt;def]
// XML : [<foo><bar>abc&lt;123&gt;def</bar></foo>]
// PAYLOAD_FROM_READER: [abc<123>def]
// PAYLOAD_FROM_DOM : [abc<123>def]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment