Flexible XML framework for Java providing comprehensive XML processing capabilities
npx @tessl/cli install tessl/maven-org-dom4j--dom4j@2.1.0DOM4J is a comprehensive XML processing framework for Java that provides a flexible and efficient API for reading, writing, and manipulating XML documents. It integrates seamlessly with XPath for powerful XML querying and fully supports DOM, SAX, JAXP standards along with Java platform features like Collections.
The library offers both tree-based and event-driven XML processing models, making it suitable for a wide range of XML manipulation tasks from simple document parsing to complex XML transformations. With support for namespace handling, XML Schema validation, and XSLT processing, DOM4J serves as a complete solution for Java applications requiring robust XML functionality.
Version: 2.1.4
License: Plexus
Java Compatibility: Java 8+
Total API Classes: 181
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.4</version>
</dependency>implementation 'org.dom4j:dom4j:2.1.4'import org.dom4j.*;
import org.dom4j.io.SAXReader;
// Parse XML from file
SAXReader reader = new SAXReader();
Document document = reader.read(new File("example.xml"));
// Get root element
Element root = document.getRootElement();
// Navigate and access content
String name = root.getName();
String text = root.getText();
List<Element> children = root.elements();import org.dom4j.*;
// Create new document
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
// Add elements and attributes
Element person = root.addElement("person")
.addAttribute("id", "1")
.addText("John Doe");
// Output XML
String xml = document.asXML();import org.dom4j.XPath;
// Create XPath expression
XPath xpath = DocumentHelper.createXPath("//person[@id='1']");
// Execute query
Node result = xpath.selectSingleNode(document);
List<Node> nodes = xpath.selectNodes(document);DOM4J is organized into several focused packages providing different aspects of XML processing functionality:
DOM4J uses a comprehensive node type system with constants defined in the Node interface:
// Node type constants
public interface Node {
short ANY_NODE = 0;
short ELEMENT_NODE = 1;
short ATTRIBUTE_NODE = 2;
short TEXT_NODE = 3;
short CDATA_SECTION_NODE = 4;
short ENTITY_REFERENCE_NODE = 5;
short PROCESSING_INSTRUCTION_NODE = 7;
short COMMENT_NODE = 8;
short DOCUMENT_NODE = 9;
short DOCUMENT_TYPE_NODE = 10;
short NAMESPACE_NODE = 13;
short UNKNOWN_NODE = 14;
short MAX_NODE_TYPE = 14;
}The node hierarchy follows this structure:
DOM4J provides centralized object creation through factories:
The Visitor interface enables type-safe tree traversal and processing:
document.accept(new VisitorSupport() {
public void visit(Element element) {
// Process elements
}
public void visit(Attribute attribute) {
// Process attributes
}
});QName and Namespace instances are cached and reused to minimize memory usage.
Full XPath 1.0 support through Jaxen integration enables powerful document querying:
// XPath with namespace support
XPath xpath = DocumentHelper.createXPath("//ns:element");
xpath.setNamespaceURIs(Map.of("ns", "http://example.com/ns"));
List<Node> results = xpath.selectNodes(document);DOM4J defines several specific exception types:
import org.dom4j.DocumentException;
import org.dom4j.InvalidXPathException;
import org.dom4j.XPathException;
import org.dom4j.IllegalAddException;
try {
Document doc = reader.read(file);
XPath xpath = DocumentHelper.createXPath("//element");
List<Node> nodes = xpath.selectNodes(doc);
} catch (DocumentException e) {
// Handle parsing errors
} catch (InvalidXPathException e) {
// Handle invalid XPath syntax
} catch (XPathException e) {
// Handle XPath evaluation errors
}// Custom SAX content handler
SAXContentHandler contentHandler = new SAXContentHandler();
// Use with any SAX parser
saxParser.setContentHandler(contentHandler);
Document document = contentHandler.getDocument();// Convert from W3C DOM to DOM4J
DOMReader domReader = new DOMReader();
Document dom4jDoc = domReader.read(w3cDocument);
// Convert from DOM4J to W3C DOM
DOMWriter domWriter = new DOMWriter();
org.w3c.dom.Document w3cDoc = domWriter.write(dom4jDoc);// Read from STAX event stream
STAXEventReader staxReader = new STAXEventReader();
Document document = staxReader.readDocument(xmlEventReader);DOM4J provides comprehensive XML namespace support:
// Define namespaces
Namespace ns1 = Namespace.get("prefix", "http://example.com/ns1");
Namespace ns2 = Namespace.get("http://example.com/ns2");
// Create namespaced elements
Element root = DocumentHelper.createElement(QName.get("root", ns1));
Element child = root.addElement(QName.get("child", ns2));
// Namespace-aware queries
XPath xpath = DocumentHelper.createXPath("//prefix:element");
xpath.setNamespaceURIs(Map.of("prefix", "http://example.com/ns1"));This documentation is organized into focused sections covering different aspects of DOM4J:
// Read configuration XML
SAXReader reader = new SAXReader();
Document config = reader.read("config.xml");
// Extract configuration values
String dbUrl = config.valueOf("//database/@url");
String poolSize = config.valueOf("//connection-pool/@size");// Create SOAP envelope
Document soap = DocumentHelper.createDocument();
Element envelope = soap.addElement("soap:Envelope")
.addNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
Element body = envelope.addElement("soap:Body");
Element operation = body.addElement("myOperation")
.addNamespace("", "http://myservice.example.com/");// Transform XML structure
for (Element element : root.elements("item")) {
String id = element.attributeValue("id");
String value = element.getText();
Element newItem = newRoot.addElement("product")
.addAttribute("productId", id)
.addElement("name").addText(value);
}DOM4J's comprehensive API and flexible architecture make it an excellent choice for any Java application requiring robust XML processing capabilities.