or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced-features.mdcore-api.mddocument-creation.mdindex.mdio-operations.mdxpath.md
tile.json

tessl/maven-org-dom4j--dom4j

Flexible XML framework for Java providing comprehensive XML processing capabilities

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.dom4j/dom4j@2.1.x

To install, run

npx @tessl/cli install tessl/maven-org-dom4j--dom4j@2.1.0

index.mddocs/

DOM4J - Flexible XML Framework for Java

Overview

DOM4J 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

Package Information

  • Package Name: dom4j
  • Package Type: maven
  • Language: Java
  • Installation: Add to Maven or Gradle dependencies (see below)

Core Imports

import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;

Dependencies

Maven

<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.1.4</version>
</dependency>

Gradle

implementation 'org.dom4j:dom4j:2.1.4'

Basic Usage

Basic XML Parsing

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();

Creating XML Documents

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();

XPath Queries

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);

Package Architecture

DOM4J is organized into several focused packages providing different aspects of XML processing functionality:

Core Packages

  • org.dom4j: Core interfaces and classes (Node, Document, Element, etc.)
  • org.dom4j.tree: Default implementations of core interfaces
  • org.dom4j.io: XML input/output operations (SAXReader, XMLWriter)
  • org.dom4j.xpath: XPath processing and pattern matching

Integration Packages

  • org.dom4j.bean: JavaBean reflection-based XML binding
  • org.dom4j.datatype: XML Schema datatype support
  • org.dom4j.dom: W3C DOM interoperability
  • org.dom4j.jaxb: JAXB integration support

Utility Packages

  • org.dom4j.dtd: DTD processing and validation
  • org.dom4j.rule: XSLT-style pattern matching and rule processing
  • org.dom4j.swing: Swing UI component integration
  • org.dom4j.util: General utility classes

Node Type Hierarchy

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:

  • Node (root interface)
    • Branch (nodes that can contain children)
      • Document (root document)
      • Element (XML elements)
    • CharacterData (text-based nodes)
      • Text (text nodes)
      • CDATA (CDATA sections)
      • Comment (XML comments)
    • Attribute (element attributes)
    • ProcessingInstruction (processing instructions)
    • Entity (entity references)
    • DocumentType (DOCTYPE declarations)

Key Design Patterns

Factory Pattern

DOM4J provides centralized object creation through factories:

  • DocumentFactory: Creates all DOM4J tree objects with customization support
  • DocumentHelper: Static utility methods for common operations

Visitor Pattern

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
    }
});

Flyweight Pattern

QName and Namespace instances are cached and reused to minimize memory usage.

XPath Integration

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);

Thread Safety Considerations

  • Node instances: NOT thread-safe for modification operations
  • Factory instances: Thread-safe for read operations and object creation
  • Cached objects: QName and Namespace are immutable and thread-safe
  • DocumentFactory singleton: Thread-safe
  • XPath expressions: Thread-safe for execution but not for configuration changes

Exception Handling

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
}

Performance Considerations

Memory Optimization

  • Use DocumentFactory.getInstance() for standard operations
  • QName and Namespace instances are automatically cached
  • Consider using SAX-based processing for very large documents

Processing Efficiency

  • XPath expressions are compiled once and can be reused
  • Use Iterator-based navigation for memory efficiency
  • Consider streaming approaches for large document processing

Integration Capabilities

SAX Integration

// Custom SAX content handler
SAXContentHandler contentHandler = new SAXContentHandler();
// Use with any SAX parser
saxParser.setContentHandler(contentHandler);
Document document = contentHandler.getDocument();

DOM Interoperability

// 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);

STAX Support

// Read from STAX event stream
STAXEventReader staxReader = new STAXEventReader();
Document document = staxReader.readDocument(xmlEventReader);

Namespace Support

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"));

Documentation Sections

This documentation is organized into focused sections covering different aspects of DOM4J:

  • Core API: Node hierarchy, Document, Element, and Attribute interfaces
  • Document Creation: DocumentHelper, DocumentFactory, and XML parsing
  • I/O Operations: Reading/writing with SAXReader, XMLWriter, STAX integration, HTML output, and JAXP compatibility
  • XPath: XPath interface, DefaultXPath, and advanced querying
  • Advanced Features: JAXB integration, bean binding, validation, utility classes, and specialized packages

Common Use Cases

Configuration File Processing

// 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");

XML Generation for Web Services

// 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/");

Data Transformation

// 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.