Spring '10 Force.com Platform Release
XML DOM Parsing

New DOM (Document Object Model) classes are available to help you to parse or generate XML content. Use the Document class to process the content in the body of an XML document. Use the XmlNode class to work with a node in an XML document. You can use these classes to create the body of a request for HttpRequest or to parse a response accessed by HttpResponse.

Here's a snippet that parsing some incoming XML:

HttpRequest req = new HttpRequest();
....
HttpResponse res = h.send(req);

dom.Document doc = res.getBodyDocument();
invoices = new List();
for(dom.XmlNode node : doc.getRootElement().getChildElements()) {
  if(node.getName()=='invoice') {
     Invoice iv = new Invoice();
     iv.from_xml(node);
     invoices.add(iv);
   }
}

As you can see from the example, you use the Dom.Document class to process the XML (for example, you can use its load() method, or reference the DOM of an HTTPRequest as I do above), and the Dom.XMLNode class to work with the nodes of an XML document.

Use the existing XmlStream set of classes for a streaming API instead, if you don't need the DOM API.


Webinar Watch Webinar Watch

Notes:

See the Apex Language Reference Guide to learn more about the XML DOM support.

Other resources: