Skip to content

Instantly share code, notes, and snippets.

Created October 7, 2012 02:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3846868 to your computer and use it in GitHub Desktop.
Save anonymous/3846868 to your computer and use it in GitHub Desktop.
Supporting code snippet for chennai october meetup presentation about "Parsing XML/JSON in Apex"
// copied from here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_xml_XmlStream_reader.htm
public with sharing class WaysToParse {
static final String SAMPLE_XML = '<books><book author="Chatty">Foo bar</book>' +
'<book author="Sassy">Baz</book></books>';
/**
Apex model class for the XML structure
*/
public class Book {
String name;
String author;
}
/**
Sample for parsing using XMLStreamReader class
*/
static Book[] parseBooksViaXmlStreamReader(String xml) {
// create XmlStreamReader for the source xml
XmlStreamReader reader = new XmlStreamReader(xml);
// Collection to which books will be parsed back
List<Book> books = new List<Book>();
while(reader.hasNext()) {
// Start at the beginning of the book and make sure that it is a book
if (reader.getEventType() == XmlTag.START_ELEMENT) {
if ('Book' == reader.getLocalName()) {
// Parse through the XML, determine the author and the characters
Book book = new Book();
book.author = reader.getAttributeValue(null, 'author');
while(reader.hasNext()) {
if (reader.getEventType() == XmlTag.END_ELEMENT) {
break;
} else if (reader.getEventType() == XmlTag.CHARACTERS) {
book.name = reader.getText();
}
reader.next();
}
books.add(book);
}
}
reader.next();
}
return books;
}
static Book[] parseViaXmlDom(String xml) {
List<Book> books = new List<Book>();
XmlDom d = new XmlDom(xml);
List<XmlDom.Element> bookElems = d.getElementsByTagName('book');
for (Xmldom.Element bkElem: bookElems) {
Book b = new Book();
b.author = bkElem.getAttribute('author');
b.name = bkElem.nodeValue;
books.add(b);
}
return books;
}
static Book[] parseViaDomXmlNode(String xml) {
List<Book> books = new List<Book>();
Dom.Document d = new Dom.Document();
d.load(xml);
Dom.XmlNode root = d.getRootElement();
// if their are multiple child elements, then finding book one requires extra effort
Dom.XmlNode[] bokos = root.getChildElements();
for (Dom.XmlNode bkElem: bokos) {
Book b = new Book();
// most of the api requires namespace
b.author = bkElem.getAttribute('author', null);
b.name = bkElem.getText();
books.add(b);
}
return books;
}
/**
via FASTXMLDOM, see benchmarks here : http://www.tgerm.com/2010/04/fast-xml-dom-vs-xmldom-benchmarking.html
*/
static Book[] parseViaFastXmlDom(String xml) {
List<Book> books = new List<Book>();
TG_XmlDom dom = new TG_XmlDom(xml);
TG_XmlNode root = dom.root;
List<TG_XmlNode> bokos = root.getElementsByTagName('book');
for (TG_XmlNode bkElem: bokos) {
Book b = new Book();
b.author = bkElem.getAttribute('author');
b.name = bkElem.nodeValue;
books.add(b);
}
return books;
}
// Test XMLSTREAMREADER
static testMethod void testParseBooksViaXmlStreamReader() {
Book[] books = parseBooksViaXmlStreamReader(SAMPLE_XML);
verifyParseResults(books);
}
// Test XMLDOM
static testMethod void testParseViaXmlDom() {
Book[] books = parseViaXmlDom(SAMPLE_XML);
verifyParseResults(books);
}
// Test DOM.XMLNODE
static testMethod void testParseViaXmlNode() {
Book[] books = parseViaDomXmlNode(SAMPLE_XML);
verifyParseResults(books);
}
// Test TG_XMLDOM
static testMethod void testParseViaFastXmlDom() {
Book[] books = parseViaFastXmlDom(SAMPLE_XML);
verifyParseResults(books);
}
static void verifyParseResults(Book[] books) {
System.assertEquals(2, books.size());
System.assertEquals('Chatty', books[0].author);
System.assertEquals('Foo bar', books[0].name);
System.assertEquals('Sassy', books[1].author);
System.assertEquals('Baz', books[1].name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment