XML Stream writing in Apex

As the popularity of Apex and Visual Force continue to increase, I am seeing more and more requirements to develop custom solutions on the Force.com platform. This is not surprising, of course, especially when you consider the great quote from A Field of Dreams which states If you build it they will come. Perhaps, also not surprising is the common requirement to integrate with other systems.

Most of the integrations which I deal with involve web service connectivity. A current project includes the requirement to send complex XML files based on Salesforce data to the organizations internal systems for processing. Luckily, the APEX language has done a great job of providing XML stream reading and writing capabilities through the XMLStreamReader and XMLStreamWriter class, both based on their Java counterparts of the same name. (Great job not reinventing the wheel here when the Java classes worked like a charm!)

Lets say we our application accepted Account name and address from a simple XML file that wanted to perform some logic on each account record (such as updating address details on our Salesforce Account object).

Let’s create a quick test method with our XML string (remember test first!):

static testMethod void testBookParser() {
  SimpleXMLExample demo = new SimpleXMLExample();
   String str = ‘<accounts>+
      <account name="Fred Flinstone">1 State Road, Bedrock</account>+
      <account name="Barney Rubble">2 Boulder Drive, Bedrock</account>+
  XmlStreamReader reader = new XmlStreamReader(str);
  TempAccount[] tmpaccts = demo.parseBooks(reader);
   System.assert(tmpaccts.size() == 2);    
   System.assert(tmpaccts[0].name = ‘Fred Flintstone’);
   System.assert(tmpaccts[2].name = ‘Barney Rubble’);

Now that we have out testmethod created we know the structure of out code, so we can start writing a class that uses the XMLStreamReader to parse the account objects:

public class SimpleXMLExample {

   public class TempAccount {
      String name;
      String address;

   TempAccount[] parseAccounts(XMLStreamReader reader) {
       TempAccount[] accts = new TempAccount[0];
       while (reader.hasNext()) {
           if (reader.getEventType() == XmlTag.START_ELEMENT {
              if(reader.getLocalName() == ‘account’) {
        return accts;

      TempAccount parseAccount(XmlStreamReader reader) {
          TempAccount tmpacct = new TempAccount();
           tmpacct.name = reader.getAttributeValue(”, ‘name’);
           while(reader.hasNext()) {
                if (reader.getEventType() == XmlTag.END_ELEMENT) {
                } else if (reader.getEventType() == XmlTag.CHARACTERS) {
                       tmpacct.address = reader.getText();
          return tmpacct;

That’s it, we now have a working example. I only demonstrated the XMLStreamReader in this post but be sure to check out the online docs for more information including an example for the XMLStreamWriter.

November 19, 2008

Leave your comments...

XML Stream writing in Apex