User: Password:
   Keep me logged in.
Register  |  I forgot my password

Login  |  Register

Efficient Java Tools  - Listing Details

ID:222
Title:DOM4J
Pagerank:4
Short Description:
Listing Thumbnail

dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP.

Description:

This page attempts to survey the landscape of available XML object models and compare and contrast their features. The information in this table is correct to the best of our knowledge and we will try and keep this information as up to date as possible. If you think there's anything wrong, please let us know here.

Feature WC3 DOM DOM4J 1.5 JDOM 1.0 XOM 1.O
Open Source Yes Yes Yes Yes
Based on Java Interfaces Yes Yes No No
Supports Java 2 Collections No Yes Yes No
Can use any SAX parser and XMLFilter Yes (usually) Yes Yes Yes
Convert to and from DOM trees Yes Yes Yes Yes
Implements DOM interfaces Yes Yes (optional) No No
Integrated XPath API support No Yes No No
Bundled XPath implementation No Yes Optional No
Support for JAXP/TrAX for XSLT integration Yes Yes Yes Yes
Capable of processing a continuous XML streams Don't know Yes No Yes
Capable of processing massive documents Don't know Yes No Yes
XML Schema Data Type support No Yes No Don't know
XInclude support Don't know No No Yes
Canonical XML support Don't know No No Yes

Fast Looping

If you ever have to walk a large XML document tree then for performance we recommend you use the fast looping method which avoids the cost of creating an Iterator object for each loop

Powerful Navigation with XPath

In dom4j XPath expressions can be evaluated on the Document or on any Node in the tree (such as Attribute, Element or ProcessingInstruction). This allows complex navigation throughout the document with a single line of code

Using Iterators

A document can be navigated using a variety of methods that return standard Java Iterators

Converting to and from Strings

If you have a reference to a Document or any other Node such as an Attribute or Element, you can turn it into the default XML text via the asXML() method.

        Document document = ...;

        String text = document.asXML();

If you have some XML as a String you can parse it back into a Document again using the helper method DocumentHelper.parseText()

        String text = "<person> <name>James</name> </person>";

        Document document = DocumentHelper.parseText(text);

Styling a Document with XSLT

Applying XSLT on a Document is quite straightforward using the JAXP API from Sun. This allows you to work against any XSLT engine such as Xalan or SAXON. Here is an example of using JAXP to create a transformer and then applying it to a Document.

Writing a document to a file

A quick and easy way to write a Document (or any Node) to a Writer is via the write() method.

  FileWriter out = new FileWriter( "foo.xml" );

  document.write( out );

If you want to be able to change the format of the output, such as pretty printing or a compact format, or you want to be able to work with Writer objects or OutputStream objects as the destination, then you can use the XMLWriter class.

Creating a new XML document

Often in dom4j you will need to create a new document from scratch. Here's an example of doing that.

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

 

public class Foo {

 

    public Document createDocument() {

        Document document = DocumentHelper.createDocument();

        Element root = document.addElement( "root" );

 

        Element author1 = root.addElement( "author" )

            .addAttribute( "name", "James" )

            .addAttribute( "location", "UK" )

            .addText( "James Strachan" );

 

        Element author2 = root.addElement( "author" )

            .addAttribute( "name", "Bob" )

            .addAttribute( "location", "US" )

            .addText( "Bob McWhirter" );

 

        return document;

    }

}

Parsing XML

One of the first things you'll probably want to do is to parse an XML document of some kind. This is easy to do in dom4j. The following code demonstrates how to this.

import java.net.URL;

 

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.io.SAXReader;

 

public class Foo {

 

    public Document parse(URL url) throws DocumentException {

        SAXReader reader = new SAXReader();

        Document document = reader.read(url);

        return document;

    }

}

Category:XML
Link Owner:
Date Added:May 05, 2010 07:00:44 PM
Number Hits:4
URL:    http://sourceforge.net/projects/dom4j/files/
Ratings
You must be logged in to leave a rating.
Average rating: (0 votes)
Reviews

No Reviews Yet.


You must be logged in to leave a Comment.