Using DOM4J for handling XML, XPATH, XSLT

Hi All,

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.

[Download Link]

Parsing XML
First thing everyone usually wants to do is to parse the XML document, which is very easy with DOM4J. Simple pass URL object of the XML document, InputStream, File object. see below

 public Document parse(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document =;
        return document;

Navigating XML Document.

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

public void bar(Document document) throws DocumentException {
Element root = document.getRootElement();

// Step 1:
// iterate through child elements of root

for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
Element element = (Element);
// do something

//Step 2:
// iterate through child elements of root with element name “foo”

for ( Iterator i = root.elementIterator( “foo” ); i.hasNext(); ) {
Element foo = (Element);
// do something

//Step 3:
// iterate through attributes of root

for ( Iterator i = root.attributeIterator(); i.hasNext(); ) {
Attribute attribute = (Attribute);
// do something

Using 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. For example.

public void bar(Document document) {
List list = document.selectNodes( “//foo/bar” );
Node node = document.selectSingleNode( “//foo/bar/author” );
String name = node.valueOf( “@name” );

For example if you wish to find all the hypertext links in an XHTML document the following code would do the trick.

public void findLinks(Document document) throws DocumentException {
List list = document.selectNodes( “//a/@href” );
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Attribute attribute = (Attribute);
String url = attribute.getValue();

NOTE: For fast navigation we can make one recursive method to navigate fast through XML document.

   public void treeWalk(Document document) {
        treeWalk( document.getRootElement() );

    public void treeWalk(Element element) {
        for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
            Node node = element.node(i);
            if ( node instanceof Element ) {
                treeWalk( (Element) node );
            else {
                // do something....

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.

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

import org.dom4j.Document;

public class Foo {

public Document styleDocument(Document document, String stylesheet)throws Exception {

// load the transformer using JAXP
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(
new StreamSource( stylesheet )

// now lets style the given document
DocumentSource source = new DocumentSource( document );
DocumentResult result = new DocumentResult();
transformer.transform( source, result );

// return the transformed document
Document transformedDoc = result.getDocument();
return transformedDoc;

I hope this would give some help for the beginners who are getting ready to play with XML’s.

R Vashi

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.