Java Tutorial - Java Scipt : JDOM Parser Test

Java Tutorial - Java Scipt :

JDOM Parser Test


JDOM is one of the two best high-level tools for working with XML documents, along with dom4j. The two have different design philosophies. JDOM focuses on providing Java-centric interfaces, while dom4J is more inclined to forgo syntactic niceness in favor of strong design patterns. Keep an eye on the effortless manner with which JDOM handles creation of XML documents and elements.

Installation

The latest JDOM release can be downloaded from the Web site (http:// www.jdom.org). Installation is very straightforward. After downloading the file, extract the files into your JDOM directory. On Windows, that could be something like c:\java\jdom. On Linux, something like /usr/local/jdom would be appropriate. Be sure to set the JDOM_HOME environment variable to point to the directory where the JDOM files are located.

Code Examples

JDOM goes out of its way to make things work the way you would expect from a Java object viewpoint. The code example shows how easy it is to create new XML documents and elements, as well as performing I/O. Here’s the source for jdom_test.java

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class jdom_test {
public static void main( String args[] ) {
// --- Creating an XML document ---
Document oDocument = new Document( new Element( “order” ) );
Element oLineItem = new Element( “line_item” );
oLineItem.setAttribute( “name”, “T-shirt” );
oLineItem.setAttribute( “count”, “1” );
oLineItem.setAttribute( “color”, “red” );
oLineItem.setAttribute( “size”, “XL” );
oLineItem.addContent(
new Element( “notes” ).setText(
“Please ship before next week.” ) );
oDocument.getRootElement().addContent( oLineItem );
System.out.println( “Document after creation: “ + oDocument );
// --- Saving the XML to a file ---
XMLOutputter oOutputter = new XMLOutputter();
try {
oOutputter.output( oDocument,
new FileWriter( “order.xml” ) );
}
Adding XML Power Tools 455
catch( Exception e ) {
e.printStackTrace();
}
// --- Loading the XML document back from the file ---
oDocument = null;
System.out.println( “Document after nulling: “ + oDocument );
SAXBuilder oBuilder = new SAXBuilder();
try {
oDocument = oBuilder.build( new FileReader( “order.xml” ) );
}
catch( Exception e ) {
e.printStackTrace();
}
System.out.println( “Document after loading: “ + oDocument );
}
}

To compile the example on Windows, use the following command:

javac –classpath .;%JDOM_HOME%\build\jdom.jar jdom_test.java

On Linux, it is:

javac –classpath .:$JDOM_HOME/build/jdom.jar jdom_test.java

To run the example after you’ve compiled it, run the following command on Windows:

java –classpath .;%JDOM_HOME%\build\jdom.jar jdom_test

On Linux, the command is:
java –classpath .:$JDOM_HOME/build/jdom.jar jdom_test

The program should generate output very similar to the dom4j example. In fact, the JDOM and dom4j examples do exactly the same thing, using a different tool. The important thing is to compare and contrast the code for the two. In my opinion, the code using JDOM is cleaner and easier to follow.