XML and JSON for Ajax - dom4j
(Page 4 of 4 )
dom4j is an XML library similar in intent to JDOM. After downloading dom4j from http://www.dom4j.org/download.html and installing it in your application's WEB-INF/lib directory, you can use it to create your XML document. As shown in Example 4-3, we create a document, add a root element to the document, add the elements and data to the root, and then return the document in a String.
Example 4-3. Using dom4j to create the XML document
// additional imports for dom4j
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
...
public String createDom4jXML(int key) throws IOException {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("converted-values");
Element element = root.addElement("decimal").addText(
Integer.toString(key));
element = root.addElement("hexadecimal").addText(
"0x" + Integer.toString(key, 16));
element = root.addElement("octal").addText("0" + Integer.toString(key, 8));
element = root.addElement("hyper").addText("&0x" + Integer.toString(key, 16));
element = root.addElement("binary").addText(Integer.toString(key, 2) + "B");
StringBuffer xmlDoc = null;
StringWriter sw = new StringWriter();
OutputFormat outformat = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(sw, outformat);
writer.write(document);
writer.close();
xmlDoc = sw.getBuffer();
return xmlDoc.toString();
}
The dom4j library uses the static methodDocumentHelper.createDocument()to create the XML document. The methodroot.addElement()puts a child element on the root element, andaddText()puts the data in the elements. TheOutputFormatclass is then used to format theXMLDocument, so the document looks like this:
<?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>
This step can be skipped, because it only formats the document for readability by adding linefeeds and spaces. Since humans shouldn't need to read this document (unless you are debugging), you won't need the formatting.
To use dom4j without the formatting, simply replace these two lines:
OutputFormat outformat = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(sw, outformat);
with this line:
XMLWriter writer = new XMLWriter(sw);
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
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.
|
|