XML and JSON for Ajax - Other Ways to Build the XML Document
(Page 3 of 4 )
Building an XML document by appending to a StringBuffer is a common approach, but it's far from ideal, particularly if you need to generate a large document programmatically. Fortunately, there are alternatives.
JDOM
One option is to use the JDOM library to write the XML. Download the jdom.jar file from http://www.jdom.org, and put it in your application's WEB-INF/lib directory. Then, instead of writing to a StringBuffer, use JDOM to build the XML, as shown in Example 4-2.
Example 4-2. Using JDOM to create the XML document
// additional imports needed for JDOM
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public String createJdomXML(int key) throws IOException {
Document document = new Document();
// create root node
Element root = new org.jdom.Element("converted-values");
document.setRootElement(root);
// create your node
org.jdom.Element element = new org.jdom.Element("decimal");
// add content to the node
element.addContent(Integer.toString(key));
// add your node to root
root.addContent(element);
element = new org.jdom.Element("hexadecimal");
element.addContent("0x" + Integer.toString(key, 16));
root.addContent(element);
element = new org.jdom.Element("octal");
element.addContent("0" + Integer.toString(key, 8));
root.addContent(element);
element = new org.jdom.Element("hyper");
element.addContent("&0x" + Integer.toString(key, 16));
root.addContent(element);
element = new org.jdom.Element("binary");
element.addContent(Integer.toString(key, 2) + "B");
root.addContent(element);
// output JDOM document as a String of bytes
XMLOutputter outputter = new XMLOutputter();
return outputter.outputString(document);
}
In the preceding code, we first create aDocument (org.jdom.Document), then anElementnamedrootwith theString "converted-values"as its value. That element becomes the root of the XML document. Here's what the document looks like at this point:
<converted-values>
</converted-values>
To add child elements to the root, we create new elements and add them to the root element. The code that creates thedecimal element and adds it to the root element looks like this:
org.jdom.Element element = new org.jdom.Element("decimal");
element.addContent(Integer.toString(key));
root.addContent(element);
We repeat this process until we've added all the elements to the root. Then we use anXMLOutputter to format the document into aString that we can send back to the client. The JDOM XML document now looks like this (with linefeeds and spaces added for readability):
<?xml version="1.0" encoding="UTF-8"?>
<converted-values>
<decimal>97</decimal>
<hexadecimal>0x61</hexadecimal>
<octal>0141</octal>
<hyper>&0x61</hyper>
<binary>1100001B</binary>
</converted-values>
Next: dom4j >>
More XML Tutorials Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Ajax on Java, written by Steven Douglas Olson (O'Reilly, 2007; ISBN: 0596101872). Check it out today at your favorite bookstore. Buy this book now.
|
|