I recently had the task to create some XML files based on object data and create an attachment. I’ve created XML files before in say Ruby and PHP but not in Apex. This was new to me and in the end was an easier task than I thought it would be.

First let’s create an objective. We want to create an XML file that has the following format:

1  <?XML encoding="utf-8" version="1.0" ?>
2  <catalog>
3   <product id="bk101">
4      <author>Mix Master Mike</author>
5      <title>XML In Apex</title>
6      <genre>Salesforce</genre>
7      <price>1444.95</price>
8      <publish_date>20016-12-10</publish_date>
9      <description>An in-depth look at creating applications with XML.</description>
10   </product>
11 </catalog>

This seems simple enough.

To accomplish this we can use the XmlStreamWriter Class: Information on XmlStreamWriter Class

It might make sense to look at the code as a whole that can accomplish this first. Then speak to what exactly is going on. So, here it is:

1 Xmlstreamwriter xmlProductInfo = new Xmlstreamwriter();
2
3 xmlProductInfo.writeStartDocument('utf-8','1.0');
4
5   xmlProductInfo.writeStartElement(null,'catalog', null);
6
7      xmlProductInfo.writeStartElement(null,'product',null);
8      xmlProductInfo.writeAttribute(null,null,'id','bk101');
9
10       xmlProductInfo.writeStartElement(null,'author',null);
11       xmlProductInfo.writeCharacters('Mix Master Mike');
12       xmlProductInfo.writeEndElement(); //Close author
13
14       xmlProductInfo.writeStartElement(null,'title',null);
15       xmlProductInfo.writeCharacters('XML In Apex');
16       xmlProductInfo.writeEndElement(); //Close title
17
18       xmlProductInfo.writeStartElement(null,'genre',null);
19       xmlProductInfo.writeCharacters('Salesforce');
20       xmlProductInfo.writeEndElement(); //Close genre
21
22       xmlProductInfo.writeStartElement(null,'price',null);
23       xmlProductInfo.writeCharacters('1444.95');
24       xmlProductInfo.writeEndElement(); //Close price
25
26       xmlProductInfo.writeStartElement(null,'publish_date',null);
27       xmlProductInfo.writeCharacters('20016-12-10');
28       xmlProductInfo.writeEndElement(); //Close publish_date
29
30       xmlProductInfo.writeStartElement(null,'description',null);
31       xmlProductInfo.writeCharacters('An in-depth look at creating applications with XML.');
32       xmlProductInfo.writeEndElement(); //Close price
33
34    xmlProductInfo.writeEndElement(); //Close product
35
36 xmlProductInfo.writeEndElement(); //Close catalog
37
38 xmlProductInfo.writeEndDocument(); //Close XML document26
39
40 String xmlFinalProductList = xmlProductInfo.getXmlString();
41
42 xmlProductInfo.close();

Once we have established Xmlstreamwriter object we can start using the methods available. Line 1 starts things off but line 3 is where the fun begins. Line 3 of the Apex code creates line 1 of the XML code.

After doing that let’s look at line 5 of the Apex code:

5   xmlProductInfo.writeStartElement(null,'catalog', null);

This is going to create:

2  <catalog>

This is not going to close though. Line 36 will close this:

36 xmlProductInfo.writeEndElement(); //Close catalog

Pretty straight forward? One method will start the element. Another is needed to end the element.

Now note between line 3 and line 36 of the Apex code for catalog that there are a number of elements defined. The tricky part here is keeping track and properly ending your elements. Now, my example is pretty simple. However, creating an element that contains 100+ elements can create a headaches. So, properly make comments of your ‘ends’.

Now, lets look at line 7 and 8 of the Apex code.

7      xmlProductInfo.writeStartElement(null,'product',null);
8      xmlProductInfo.writeAttribute(null,null,'id','bk101');

This will create the following element with the defined attribute:

3   <product id="bk101">

You can add multiple attributes. If I wanted to add another I could have done the following:

7      xmlProductInfo.writeStartElement(null,'product',null);
8      xmlProductInfo.writeAttribute(null,null,'id','bk101');
9      xmlProductInfo.writeAttribute(null,null,'test','ThisIsATest');

This would result in:

3   <product id="bk101" test="ThisIsATest">

Next lets look at lines 10-12:

10       xmlProductInfo.writeStartElement(null,'author',null);
11       xmlProductInfo.writeCharacters('Mix Master Mike');
12       xmlProductInfo.writeEndElement(); //Close author

This will result in:

4      <author>Mix Master Mike</author>

The key line is line 11. The creates the value within the element we have just defined.

Line 38 will end our XML document. The start was defined line 3.

38 xmlProductInfo.writeEndDocument(); //Close XML document26

Line 40 is I have defined a String variable and defined it with the XML value. Next post I’ll write how we can take this value, create the xml file, and attach it to and object.

40 String xmlFinalProductList = xmlProductInfo.getXmlString();

Line 42 closes it all!

42 xmlProductInfo.close();

Now, this is all pretty straight forward I think. The key thing to remember is keeping track where you have your start and end for each element. If you do not properly close a single one you will result in an invalid xml format.