Free, independent guides since 2010

Code

dom4j: the Java XML library explained

What dom4j is, why Java developers still use it for XML in 2026, how to add it to Maven or Gradle and where to go next: parsing, XPath, writing and security.

<guide section="Code" type="guide" read="3 min"/>

The name of this website has a history. Long before dom4j.org became a place for tips on web hosting, HTML and SEO, “dom4j” was – and still is – the name of a popular open-source library that Java developers use to work with XML. Because many visitors still arrive here looking for that library, we wrote a compact set of guides about it. They are independent: the official project home is dom4j.github.io and the source code lives on GitHub.

What is dom4j?

dom4j is a Java library for processing XML. It reads an XML file or string into a tree of objects (a document with elements, attributes and text), lets you walk or search that tree, change it, and write it back out as XML. Its name comes from the Document Object Model, the classic way of representing a document as a tree.

What made dom4j popular in the early 2000s was its developer-friendly API. Where the standard W3C DOM in Java is verbose and full of casting, dom4j feels like it was designed for Java: elements are regular Java objects, child lists work with the Collections framework, and XPath is built right into the API. Frameworks such as Hibernate used it for years, and countless enterprise applications still do.

What you can do with dom4j

  • Parse XML from files, URLs, streams or strings with SAXReader or DocumentHelper.parseText().
  • Navigate the tree with methods like getRootElement(), elements("name"), elementText() and attributeValue().
  • Query with XPath using selectNodes(), selectSingleNode() and valueOf().
  • Build new documents with DocumentHelper.createDocument() and addElement().
  • Write XML – compact or pretty-printed – with XMLWriter and OutputFormat.
  • Convert between dom4j and W3C DOM, and run XSLT transformations through the standard JAXP APIs.

Adding dom4j to your project

dom4j is published on Maven Central under the group id org.dom4j. At the time of writing (late 2026) the newest version on Maven Central is 2.2.0; the long-lived 2.1 line ended with 2.1.4. Check the release notes for the minimum Java version of the release you pick.

XML
<!-- Maven -->
<dependency>
  <groupId>org.dom4j</groupId>
  <artifactId>dom4j</artifactId>
  <version>2.2.0</version>
</dependency>
<!-- Only needed for XPath -->
<dependency>
  <groupId>jaxen</groupId>
  <artifactId>jaxen</artifactId>
  <version>2.0.0</version>
</dependency>
Gradle (Kotlin)
// Gradle (Kotlin DSL)
dependencies {
    implementation("org.dom4j:dom4j:2.2.0")
    implementation("jaxen:jaxen:2.0.0") // for XPath
}

A first example in ten lines

Here is the shortest useful dom4j program: it reads an XML string, finds the root element and prints the title of every book.

Java
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class FirstSteps {
    public static void main(String[] args) throws Exception {
        String xml = "<library><book id='1'><title>XML basics</title></book>"
                   + "<book id='2'><title>Java and XML</title></book></library>";
        Document doc = DocumentHelper.parseText(xml);
        for (Element book : doc.getRootElement().elements("book")) {
            System.out.println(book.attributeValue("id") + ": " + book.elementText("title"));
        }
    }
}

dom4j compared with other Java XML APIs

APIModelBest for
W3C DOM (built into Java)Full tree in memoryStandard code without extra dependencies
dom4jFull tree in memory, Java-friendlyReadable code, XPath, moderate file sizes
JDOM2Full tree in memory, Java-friendlyA similar alternative to dom4j
SAXEvent stream (push)Very large files, low memory
StAXEvent stream (pull)Large files with more control than SAX
JAXB / Jakarta XML BindingXML mapped to Java classesFixed schemas you control

Because dom4j keeps the whole document in memory, it is ideal for configuration files, feeds, API responses and documents up to a few dozen megabytes. For gigabyte-sized exports, a streaming API such as StAX is the better tool.

The dom4j guides on this site

  1. How to start using dom4j – a short video tutorial to get going.
  2. Reading and parsing XML with dom4j – files, strings, navigation and encodings.
  3. Using XPath with dom4j – queries, attributes, functions and namespaces.
  4. Creating and writing XML with dom4j – building documents and pretty-printing.
  5. Secure XML parsing with dom4j – how to avoid XXE and other XML attacks.

Frequently asked questions

Is dom4j still maintained?

Yes. The project lives on GitHub under the dom4j organisation and is published to Maven Central as org.dom4j:dom4j. Releases are not frequent, but the library is stable and still receives updates.

Do I need Jaxen to use dom4j?

Only for XPath. Reading, building and writing XML work without it, but selectNodes(), selectSingleNode() and valueOf() need Jaxen on the classpath.

Is this website the official dom4j site?

No. The official project home is dom4j.github.io. dom4j.org is an independent website about building and running websites, and these pages are our own guides to the library.

In this branch

/coding/dom4j · 7 pages