Abstract
While doing some coding using the new Apex DOM classes I felt so frustrated regarding BAD BAD class design, that I decided to write a post covering my experiences. For checking the good parts of API please check my previous post.
Why DOM Document & XMLNode sucks ?
Both of the new classes Document and XMLNode miss pretty common and popular DOM methods for ex.
getElementsByTagName(String nodeName) : Why this is missing, is it tough for Salesforce developers to expose ??
getElementsByPath (String path) : Ok, this is missing too. Its tolerable 🙂
addChild(XmlNode node) : Why can’t I add a First Class XmlNode inside an XMLNode.
toXmlString() : One has to call DOM.Document.toXMLString(), this prints the whole XML. But I don’t want that big pollution in my logs.
Only relief is that XMLNode gives “getElementByTagName()” as “getChildElement()”. getElementsByPath() is not much used by everyone, but “getElementsByTagName()” is really a must have. So developers might write wrappers to use XmlNode.getChildren or XmlNode.getChildElement to get the missing api’s. But again, every wrapper is apex and every line apex line you write will add to script limits. But the fact that hurts me most is missing “addChild(XMLNODE)”, I guess this method is pretty powerful and helps a lot on many occasions, developers who deal with XML parsing know this very well :). Imagine a situation when you have a pretty complex child node that you get from an web service call and its supposed to go inside a big xml parent node. I would say salesforce API team should at least consult existing XmlDom class for its intuitive design and ease to use.
Apart from that the API and its docs both are confusing. There are two methods in Dom.XmlNode
String getAttribute(String key, String keyNamespace)
String getAttributeValue(String key, String keyNamespace)
Apex docs for both of these is same : “Returns the attribute value for the given key and keyNamespace.” So why the hell there are two APIs that do same thing, receive same arguments and return same type. Here is the link for Apex docs. For quick look here is the screenshot.

Now I felt the real pain, when I started porting a XMLDom based apex code to Dom.Document/XmlNode. I will not say anything, will just add code snippets for both, you can feel the pain. The example code below is for parsing a Google Gdata feed.
// USING XMLDOM.cls
public Integer getTotalResults(XMLDom.Element feed) {
Integer intVal = null;
String intValStr = feed.getValue('openSearch:totalResults');
if (intValStr != null) {
intVal = Integer.valueOf(intValStr);
}
return intVal;
}
// Using DOM.XmlNode
public Integer getTotalResults(Dom.XmlNode feed ) {
Integer intVal = null;
// NOTE:
// 1. I have to check if getChildElement is null
// 2. I have include namespace, it doesn't works without that.
String intValStr = feed.getChildElement('openSearch:totalResults','http://www.w3.org/2005/Atom').getText();
if (intValStr != null) {
intVal = Integer.valueOf(intValStr);
}
return intVal;
}
Though most of you already know the design of XmlDom.cls I am still sharing that below.

I still love the simple design of XmlDom.cls, and wish salesforce can create a replica of that instead reinventing the wheel and giving “Document and XmlNode”. This will help to
Reduce the efforts(Code change) required in migrating to the new DOM classes.
Reduce the “Learning curve” to almost zero.
Reduce the requirement to fix the API holes, by writing wrapper classes.
In starting I was pretty excited about this new Spring 10 API, but the excitement ended soon when I started using it. Its really a pain to use and worst design Apex API for me.
Comments (5)
Anonymoussays:
March 24, 2010 at 9:09 pmAre you saying that for EVERY getChildElement(NodeName, namespace) that I use, I have to put this IF statement? How will I ever get code coverage for that?That really sucks.
Anonymoussays:
March 25, 2010 at 4:05 am@David, Sorry I am not clear which if statement you are talking about. But yes the code becomes lengthy and unreadable if you need to include namespace or pass namespace as null. Most of the time developer is focused on easily parsing XML contents, rather being bothering about which namespaces to include to even get an attribute value. I like the W3C DOM Model, to have an additional method getElementsByTagNameNS(tagName, namespaceUri) for working with namespaces. Using namespaces for me is advanced XML parsing, that should be optional in any XML API, rather being enforced.I am developing a wrapper over this Spring 10 XML DOM API, the sole motto of this API is to ease the life of developers. Here is the link https://code.google.com/p/apex-fast-xml-dom/
Anonymoussays:
April 11, 2010 at 5:24 amHi, I've tried your wrapper classes and only notice an increase (often almost double!) in script statements compared to Ron Hess' XMLDom, which I did not expect as you are using the built-in XML parser.Have you done any comparisons of script statements yourself using identical XML documents between your wrapper classes and the original XMLDom class?
Anonymoussays:
April 11, 2010 at 6:25 am@VB Thanks for evaluating and providing feedback on Fast XML DOM. I definitely evaluated XmlDOM before starting Fast Xml Dom and afterwards during implementation also also. Here is a post that indicates governor+script statement issues with XmlDOM as compared to Spring 10 DOM Classes. http://www.tgerm.com/2010/02/apex-dom-classes-document-xmlnode.htmlXmlDOM was good at design, but the initial SAX parsing its doing for preparing the DOM sucks most of the script statements. Fast XML Dom is internally using the Spring 10 XML DOM Classes for heavy lifting so its out of script statements limit for many big things.I will do a separate blog post on some public big XML structure to show the benchmarks of both XMLDOM and Fast XML DOM. Pls stay connected.
Anonymoussays:
April 11, 2010 at 8:04 pm@VB even I will appreciate if you can tell me which API's you feel are really costly. As its in TODO list for this project to publish COST factor associated with API's. This because all of them are not coming directly via Spring 10 XML DOM Wrapper. I am doing many calculations to get upto them. Here is the todo list http://www.tgerm.com/2010/03/apex-fast-xml-dom-todos.htmlThere was slight optimization required in one API that I fixed and checked in the code to SVN. Also did some bench marking here are the results.Here is the post that shows the resultshttp://www.tgerm.com/2010/04/fast-xml-dom-vs-xmldom-benchmarking.html