Java Tutorial - Java Scipt : dom4j Parser Test

Java Tutorial - Java Scipt :

dom4j Parser Test

This code example for dom4j is functionally equivalent to the DOM parser test. The difference is that the dom4j\ shields the developer from a lot of the overhead and grunt work. The ability to specify details remains, but the default interface hides that complexity from us.


Unlike the previous examples, which rely on the bundled JAXP implementation, dom4j requires installation. This is a straightforward affair that follows the usual download and unzip model. First, go to the dom4j Web site ( and download the latest version. Then, extract the files into a directory for dom4j. The exact directory will depend on your personal preferences. On a Windows system, you can extract to something like \java\dom4j. On Linux, you could try /usr/local/dom4j. After the files are extracted, set the DOM4J_HOME environment variable to point to the directory where the files are located.

It is not recommended that you put XML library .jars into the JRE’s lib\ext directory. This is because the new .jar files can cause conflicts with .jar files that are packaged with some J2EE application containers.

Code Examples

This code example shows the design patterns that are consciously avoided by JDOM due to the syntactic overhead. In my opinion, its biggest quirk is the need to have a reference to the parent document in order to create new elements.

Although there are valid architectural reasons for this requirement, it forces us to keep references to the parent document in a lot of our code. This makes it difficult to create elements and attach them later to a document.
Here’s the source code for

import org.dom4j.*;
public class Dom4jTest {
public static void main( String args[] ) {
// --- Creating an XML document ---
Document oDocument = DocumentHelper.createDocument();
Element oOrder = oDocument.addElement( “order” );
Element oLineItem = oOrder.addElement( “line_item” );
oLineItem.addAttribute( “name”, “T-shirt” );
oLineItem.addAttribute( “count”, “1” );
oLineItem.addAttribute( “color”, “red” );
oLineItem.addAttribute( “size”, “XL” );
oLineItem.addElement( “notes” ).addText(
“Please ship before next week.” );
System.out.println( “Document after creation: “ +
oDocument + oDocument.asXML() );
// --- Save the XML to a file. ---
try {
XMLWriter oWriter = new XMLWriter(
new FileWriter( “order.xml” ) );
oWriter.write( oDocument );
Adding XML Power Tools 453
catch( Exception e ) {
// --- Load the XML document back from the file. ---
oDocument = null;
System.out.println( “Document after nulling: “ + oDocument );
SAXReader oReader = new SAXReader();
try {
oDocument = “order.xml” );
catch( Exception e ) {
System.out.println( “Document after loading: “ +
oDocument + oDocument.asXML() );

Make sure that the environment variable for DOM4J_HOME is set to point to the directory where dom4j is installed. To compile the example on Windows, run the following command:

javac –classpath .;%DOM4J_HOME%\dom4j-full.jar

On Linux, the compile command is:

javac –classpath .:$DOM4J_HOME/dom4j-full.jar

After compiling the example, you can run it with the following command on Windows:

java –classpath .;%DOM4J_HOME\dom4j-full.jar Dom4jTest

On Linux, the command is:

java –classpath .:$DOM4J_HOME/dom4j-full.jar Dom4jTest

Like the DOM example program, the dom4j example outputs the XML document after it is first constructed. Then, it parses the newly generated order.xml and shows the results of the parsing. Fairly run-of-the-mill activity, but it gives a good sense for how the dom4j interface is designed.