SAX, the Simple API for XML, provides another way to create an XML document for an Ajax application. It may be faster than JDOM or dom4J, because it doesn't require building a DOM tree for your document. Start by initializing a StringWriter and a StreamResult. Initialize the StreamResultwith theStreamWriter, then get aSAXTransformerFactoryand get aTransformerHandlerfrom that. TheTransformerHandlerallows you to create an XML document by starting a document and appending elements and data to theTransformerHandler. Example 4-4 shows how it works.
Example 4-4. Using SAX to write out the XML document
// additional imports for writing XML with SAX import java.io.*; import org.xml.sax.helpers.AttributesImpl; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler;
public String createSAXXML(int key) { Writer writer = new StringWriter(); StreamResult streamResult = new StreamResult(writer);
transformerHandler.setResult(streamResult); // start the document transformerHandler.startDocument(); // list all the attributes for element AttributesImpl attr = new AttributesImpl(); // start writing elements // every start tag and end tag has to be defined explicitly transformerHandler.startElement(null,null, "converted-values", null); transformerHandler.startElement(null,null,"decimal",null); data = Integer.toString(key, 10); transformerHandler.characters(data.toCharArray(),0,data.length());
The third element is the only element needed to set up the XML tag,<binary>.
The actualstartElement()method declaration looks like this:
public void startElement(String uri, String localName, String qName, Attributes atts)
Theuri parameter is used for the namespace, but since this example does not use a namespace, anullis passed in.
The second parameter,localName, is also used for the namespace and not needed in this example.
The third parameter,qName, is the qualified name.
The last parameter,atts, is used when the element has attributes; pass innullif attributes are not used, as in this case.
To put the data after the element tag, we set aString,data, to the desired value:
data = Integer.toString(key, 2)+"B";
Then we convert the data to aCharArrayand pass it into thecharacters()method. The second and third parameters show where processing starts and stops in theCharArray:
Each element is created withstartElement(),characters(), andendElement(). When all of the elements for the documents have been completed, a call toendDocument()is executed and the result is sent to theStreamResultthat was set up at the start of the method:
Finally, theStreamResultis converted to aStringby callingtoString()on theStringWriter. TheStreamResultwraps theStringWriterthat was set up at the beginning of this method:
return writer.toString();
TheStringcan then be returned to the calling method.
Using SAX is purportedly a faster and less memory-intensive way to create XML documents than using DOM-based libraries such as JDOM and dom4j. If your testing shows that speed is an issue, or if the SAX API is more natural for your application, you should consider using it.
There are other ways to create an XML document. For example, the Apache project's Element Construction Set (ECS) allows you to create an XML document, but there is no method to add data to the document at this time, so for this application ECS is not useful.