General

General
What is dom4j?

dom4j is an Open Source XML framework for Java. dom4j allows you to read, write, navigate, create and modify XML documents. dom4j integrates with DOM and SAX and is seamlessly integrated with full XPath support.

What is the dom4j license?

We use an Apache-style open source license which is one of the least restrictive licenses around, you can use dom4j to create new products without them having to be open source.

You can find a copy of the license here.

What do I need to add to my CLASSPATH?

The dom4j.jar only contains the dom4j classes. If you want to use a SAX parser, you'll have to include the SAX classes and the SAX parser of your choice to your CLASSPATH. If you want to use XPath expressions, you also have to include the jaxen.jar to your CLASSPATH.

dom4j can use your existing XML parser and/or DOM implementation (such as Crimson or Xerces if you want it to. dom4j can also use JAXP to configure which SAX Parser to use - just add the jaxp.jar to your CLASSPATH and whichever SAX parser you wish away you go.

How does dom4j relate to DOM?

DOM is a quite large language independent API. dom4j is a simpler, lightweight API making extensive use of standard Java APIs such as the Java 2 collections API.

Remark that dom4j fully supports the DOM standard allowing both APIs to be used easily together.

How does dom4j relate to JDOM?

dom4j is a different project and different API to JDOM though they both have similar goals. They both attempt to make it easier to use XML on the Java platform. They differ in their design, API and implementation.

dom4j is based on Java interfaces so that plug and play document object model implementations are allowed and encouraged such as small, read only, quick to create implementations or bigger, highly indexed fast to naviagte implementations or implementations which read themselves lazily from a database or Java Beans etc.

dom4j uses polymorphism extensively such that all document object types implement the Node interface. Also both the Element and Document interfaces can be used polymorphically as they both extend the Branch interface.

dom4j is fully integrated with XPath support throughout the API so doing XPath expressions is as easy as

SAXReader reader = new SAXReader();
Document document = reader.read( url );
List links = document.selectNodes( "//a[@href]" );
String title = document.valueOf( "/head/title" );

dom4j will soon provide a configuration option to support the W3C DOM API natively to avoid unnecessary tree duplication when using dom4j with XSLT engines etc.

How does dom4j work with DOM and SAX?

You can create dom4j documents from XML text, SAX events or existing DOM trees or you can write dom4j documents as SAX events, DOM trees or XML text.

Using dom4j

Using dom4j
How can I use XSLT with dom4j?

dom4j integrates with XSLT using the JAXP standard (TrAX) APIs. A dom4j Document can be used as the source of XML to be styled or the source of the stylesheet. A dom4j Document can also be used as the result of a transformation.

First you'll need to use JAXP to load a Transformer.

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.dom4j.Document;
import org.dom4j.DocumentResult;
import org.dom4j.DocumentSource;
...

TransformerFactory factory 
  = TransformerFactory.newInstance();

Transformer transformer 
  = factory.newTransformer( new StreamSource( "foo.xsl" ) );

Now that you have a transformer its easy to style a Document into a new Document.

DocumentSource source = new DocumentSource( document );
DocumentResult result = new DocumentResult();
transformer.transform( source, result );

Document transformedDoc = result.getDocument();

If you want to transform a Document into XML text you can use JAXP as follows:-

DocumentSource source = new DocumentSource( document );
DocumentResult result = new StreamResult( new FileReader( "output.xml" ) );
transformer.transform( source, result );

For more information on JAXP and (TrAX) try Sun's JAXP site.

How can I pretty print my XML document?

You can control the format of the XML text output by XMLWriter by using the OutputFormat object. You can explicitly set the various formatting options via the properties methods of the OutputFormat object. There is also a helper method OutputFormat.createPrettyPrint() which creates the default pretty-print format.

So to pretty print some XML (trimming all whitespace and indenting nicely) the following code should do the job...

    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter( out, format );
    writer.write( document );
    writer.close();
How can I parse a document from a String?

Sometimes you have a String (or StringBuffer) which contains the XML to be parsed. This can be parsed using SAXReader and the StringReader from the JDK. For example:-

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

public class Foo {

    public Document getDocument() throws DocumentException {
        return DocumentHelper.parseText( 
            "<root> <child id='1'>James</child> </root>"
        );
    }
}
      
How do I compare 2 nodes for equality?

dom4j by default uses identity based equality for performance. It avoids having to walk entire documents or document fragments when putting nodes in collections.

To compare 2 nodes (attributes, elements, documents etc) for equality the NodeComparator can be used.

    Node node1 = ...;
    Node node2 = ...;
    NodeComparator comparator = new NodeComparator();
    if ( comparator.compare( node1, node2 ) == 0 ) {
        // nodes are equal!
    }

If you are having problems comparing documents that you think are equal but the NodeComparator decides that they are different, you might find the following useful.

In dom4j/test/src/org/dom4j/AbstractTestCase.java is-a JUnit TestCase and is an abstract base class for dom4j test cases. It contains a whole bunch of useful assertion helper methods for testing documents, nodes and fragments being equal. The nice thing is that you get useful messages telling you exactly why they are different, so its pretty easy to track down. For example.

    
public MyTest extends AbstractTestCase {
    ...
    public void testSomething() {
        Document doc1 = ...;
        Document doc2 = ...;

        assertDocumentsEqual( doc1, doc2 );
        ...

        assertNodesEqual( doc1.getRootElement(), doc2.getRootElement() );
    }
}
How does dom4j handle very large XML documents?

dom4j provides an event based model for processing XML documents. Using this event based model allows developers to prune the XML tree when parts of the document have been successfully processed avoiding having to keep the entire document in memory.

For example, imagine you need to process a very large XML file that is generated externally by some database process and looks something like the following (where N is a very large number).

<ROWSET>
    <ROW id="1">
        ...
    </ROW>
    <ROW id="2">
        ...
    </ROW>
    ...
    <ROW id="N">
        ...
    </ROW>
</ROWSET>     
      

We can process each <ROW> at a time, there is no need to keep all of them in memory at once. dom4j provides a Event Based Mode for this purpose. We can register an event handler for one or more path expressions. These handlers will then be called on the start and end of each path registered against a particular handler. When the start tag of a path is found, the onStart method of the handler registered to the path is called. When the end tag of a path if found, the onEnd method of the handler registered to that path is called.

The onStart and onEnd methods are passed an instance of an ElementPath, which can be used to retrieve the current Element for the given path. If the handler wishes to "prune" the tree being built in order to save memory use, it can simply call the detach() method of the current Element being processed in the handlers onEnd() method.

So to process each <ROW> individually we can do the following.

// enable pruning mode to call me back as each ROW is complete
SAXReader reader = new SAXReader();
reader.addHandler( "/ROWSET/ROW", 
    new ElementHandler() {
        public void onStart(ElementPath path) {
            // do nothing here...    
        }
        public void onEnd(ElementPath path) {
            // process a ROW element
            Element row = path.getCurrent();
            Element rowSet = row.getParent();
            Document document = row.getDocument();
            ...
            // prune the tree
            row.detach();
        }
    }
);

Document document = reader.read(url);

// The document will now be complete but all the ROW elements
// will have been pruned.
// We may want to do some final processing now
...
      
Does dom4j support the Visitor Pattern?

Yes. dom4j supports the visitor pattern via the Visitor interface.

Here is an example.

protected void foo(Document doc) {
  
    // lets use the Visitor Pattern to 
    // navigate the document for entities

    Visitor visitor = new VisitorSupport() {
        public void visit(Entity entity) {
            System.out.println( 
                "Entity name: " + entity.getName() 
                + " text: " + entity.getText() 
            );
        }
    };

    doc.accept( visitor );
}
      
Can I sort the List returned by Node.selectNodes()?

Yes. The selectNodes() is a really useful feature to allow nodes to be selected from any object in the dom4j object model via an XPath expression. The List that is returned can be sorted by specifying another XPath expression to use as the sorting comparator.

For example the following code parses an XML play and finds all the SPEAKER elements sorted in name order.

SAXReader reader = new SAXReader();
Document document = reader.read( new File( "xml/much_ado.xml" ) );
List speakers = document.selectNodes( "//SPEAKER", "." );
      

In the above example the name of the SPEAKER is defined by the XPath expression "." as the name is stored in the text of the SPEAKER element. If the name was defined by an attribute called "name" then the XPath expression "@name" should be used for sorting.

You may wish to remove duplicates while sorting such that (for example) the distinct list of SPEAKER elements is returned, sorted by name. To do this add an extra parameter to the selectNodes() method call.

List distinctSpeakers = document.selectNodes( "//SPEAKER", ".", true );
      
What features are optional in dom4j?

In dom4j being able to navigate up a tree towards the parent and to be able to change a tree are optional features. These features are optional so that an implementation can create memory efficient read only document models which conserve memory by sharing imutable objects (such as interning Atttributes).

There are some helper methods to determine if optional features are implemented. Here is some example code demonstrating their use.

protected void foo(Node node) {
  
    // can we do upward navigation?
    if ( ! node.supportsParent() ) {
        throw new UnsupportedOperationException(
          "Cannot navigate upwards to parent"
        );
    }
    Element parent = node.getParent();

    System.out.println( "Node: " + node 
        + " has parent: " + parent 
    );

    if ( parent != null ) {

        // can I modify the parent?
        if ( parent.isReadOnly() ) {
            throw new UnsupportedOperationException(
              "Cannot modify parent as it is read only"
            );
        }

        parent.setAttributeValue( "bar", "modified" );
    }
}
      
What does the following mean 'Warning: Error occurred using JAXP to load a SAXParser. Will use Aelfred instead'

If dom4j detects JAXP on the classpath it tries to use it to load a SAX parser. If it can't load the SAX parser via JAXP it then tries to use the org.xml.sax.driver system property to denote the SAX parser to use. If none of the above work dom4j outputs a warning and continues, using its own internal Aelfred2 parser instead.

The following warning is a result of JAXP being in the classpath but either an old JAXP1.0 version was found (rather than JAXP 1.1) or there is no JAXP configured parser (such as crimson.jar or xerces.jar) on the classpath.

Warning: Error occurred using JAXP to load a SAXParser. Will use Aelfred instead

So the warning generally indicates an incomplete JAXP classpath and is nothing to worry excessively about. If you'd like to see the full verbose reason why the load of a JAXP parser failed then you can try setting the system property org.dom4j.verbose=true. e.g.

java -Dorg.dom4j.verbose=true MyApp

And you should see a verbose list of why the load of a SAX parser via JAXP failed.

To avoid this warning happening either remove the jaxp.jar from your classpath or add a JAXP 1.1. jaxp.jar together with a JAXP 1.1 parser such as crimson.jar or xerces.jar to your classpath.

What XML parser does dom4j use?

dom4j works with any SAX parser via JAXP. So putting a recent distribution of crimson.jar or xerces.jar on the CLASSPATH will allow Crimson or Xerces's parser to be used.

If no SAX parser is on the classpath via JAXP or the SAX org.xml.sax.driver system property then the embedded Aelfred distribution will be used instead. Note that the embedded Aelfred distribution is a non validating parser, though it is quite fast

How can I validate my document?

If a recent version of crimson.jar or xerces.jar is on the CLASSPATH then dom4j will use that as the SAX parser via JAXP. If none of these are on the CLASSPATH then a bundled version of Aelfred is used, which does not validate.

So to perform DTD validation when parsing put crimson.jar or xerces.jar on the CLASSPATH. If you wish to validate against an XML Schema then try xerces.jar. Then use the following code.

// turn validation on
SAXReader reader = new SAXReader(true);
Document document = reader.read( "foo.xml" );

Note: if you want to validate against an XML Schema with xerces, you need to enable the XML Schema validation with the "setFeature" method. For more information about xerces features visit the xerces website. Below is a code sample to enable XML Schema validation.

// turn validation on
SAXReader reader = new SAXReader(true);
// request XML Schema validation
reader.setFeature("http://apache.org/xml/features/validation/schema", true);
Document document = reader.read( "foo.xml" );

An alternative approach is to use Sun's MSV library for validation, which allows you to use DTD, XML Schema, Relax NG, Relax or TREX as the schema languages. There's an example in the daily build at dom4j/src/samples/validate/JARVDemo.java

If you are validating an existing dom4j document then we recommend you try MSV as it avoids turning the document into text and then parsing it again - MSV can work purely off of SAX events generated from the dom4j document.

Using this approach your code will actually be based on the JARV API which allows alternative validation mechanisms to be plugged into your code.

How do I import dom4j into VAJ?

VisualAge for Java checks all dependencies in a JAR and displays warnings if there are any unresolved links. To avoid any warnings the following steps should be followed (thanks to Jan Haluza for this).

  1. Uninstall all the packages having anything in common with the xml (com.ibm.xml* , org.w3c.dom ..) (these packages contains older definitions DOM ver. 1 ...
  2. Install the following jars
    dom4j.jar
    xalan.jar
    PullParser.jar  
    relaxng.jar
    msv.jar
    isorelax.jar
    xsdlib.jar
    crimson.jar
    
Cannot find DTD; how can I tell dom4j where to find the DTD from a DOCTYPE?

A common way around this is to implement a SAX EntityResolver to load the DTD from somewhere else. e.g. you could include the DTD in your JAR with your java code and load it from there.

EntityResolver resolver = new EntityResolver() {
    public InputSource resolveEntity(String publicId, String systemId) {
        if ( publicId.equals( "-//Acme//DTD Foo 1.2//EN" ) ) {
            InputStream in = getClass().getResourceAsStream(
                "com/acme/foo.dtd"
            );
            return new InputSource( in );
        }
        return null;
    }
};

SAXReader reader = new SAXReader();
reader.setEntityResolver( resolver );
Document doc = reader.parse( "foo.xml" );