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
SAXReaderorDocumentHelper.parseText(). - Navigate the tree with methods like
getRootElement(),elements("name"),elementText()andattributeValue(). - Query with XPath using
selectNodes(),selectSingleNode()andvalueOf(). - Build new documents with
DocumentHelper.createDocument()andaddElement(). - Write XML – compact or pretty-printed – with
XMLWriterandOutputFormat. - 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.
<!-- 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 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.
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
| API | Model | Best for |
|---|---|---|
| W3C DOM (built into Java) | Full tree in memory | Standard code without extra dependencies |
| dom4j | Full tree in memory, Java-friendly | Readable code, XPath, moderate file sizes |
| JDOM2 | Full tree in memory, Java-friendly | A similar alternative to dom4j |
| SAX | Event stream (push) | Very large files, low memory |
| StAX | Event stream (pull) | Large files with more control than SAX |
| JAXB / Jakarta XML Binding | XML mapped to Java classes | Fixed 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
- How to start using dom4j – a short video tutorial to get going.
- Reading and parsing XML with dom4j – files, strings, navigation and encodings.
- Using XPath with dom4j – queries, attributes, functions and namespaces.
- Creating and writing XML with dom4j – building documents and pretty-printing.
- 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
- <code/>How to start using dom4jA video tutorial on getting started with dom4j: adding it to a Java project, creating a document, adding elements and pretty-printing XML.
- <code/>Reading and parsing XML with dom4jHow to read XML with dom4j: parsing files and strings with SAXReader, walking elements and attributes, handling encodings and dealing with parse errors.
- <code/>Using XPath with dom4jHow to query XML with XPath in dom4j: selectNodes, selectSingleNode and valueOf, attribute filters, XPath functions, compiled expressions and namespaces.
- <code/>Creating and writing XML with dom4jBuild XML documents in Java with dom4j: create elements and attributes, add text and CDATA, modify existing documents and write pretty-printed UTF-8 files.
- <code/>Secure XML parsing with dom4jHow to protect Java applications that use dom4j against XXE, entity expansion and other XML attacks, with safe SAXReader settings for every version.
- <code/>JSON and XML compared: when to use whichThe differences between JSON and XML, with examples: syntax, data types, attributes and namespaces, schemas, tooling, and when each format is the better choice for websites and APIs.
- <code/>XML basics: elements, attributes and well-formed documentsA beginner’s introduction to XML: elements and attributes, the XML declaration, well-formedness rules, namespaces, entities and CDATA, and where XML is used on the web.