Last active
August 29, 2015 14:15
-
-
Save richardvanhook/8b29c27b66adb3fb65f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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<123>def] | |
// XML : [<foo><bar>abc<123>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