Introduction

Welcome to DomTrip - the lossless XML editing library for Java that preserves every detail of your XML documents during round-trip operations.

What is DomTrip?

DomTrip is a Java library designed to solve a fundamental problem with XML processing: information loss during parsing and serialization. Unlike traditional XML libraries that focus on data extraction, DomTrip preserves the complete original formatting, making it perfect for XML editing scenarios where you need to maintain the exact structure and style of the original document.

Why DomTrip?

๐ŸŽฏ Perfect Round-Trip Preservation

String originalXml = createTestXml("root");
Document doc = Document.of(originalXml);
Editor editor = new Editor(doc);
String result = editor.toXml();
// result is IDENTICAL to originalXml if no modifications were made

๐Ÿ”ง Intelligent Editing

// Add new elements while preserving original formatting
String xml = createMavenPomXml();
Document doc = Document.of(xml);
Editor editor = new Editor(doc);
Element parent = editor.root().descendant("dependencies").orElseThrow();
Element newDep = editor.addElement(parent, "dependency");
editor.addElement(newDep, "groupId", "org.example");

๐Ÿš€ Modern Java API

// Fluent builders and Stream-based navigation
Element element = Element.of("dependency").attribute("scope", "test");
element.addNode(Element.text("groupId", "junit"));

String xml = createTestXml("root");
Document doc = Document.of(xml);
Element root = doc.root();
Optional<Element> child = root.child("dependency");
Stream<Element> descendants = root.descendants();

Key Features

  • ๐Ÿ”„ Lossless Round-Trip: Preserves comments, whitespace, entity encoding, attribute quotes
  • ๐Ÿ“ Format Preservation: Only modified sections are reformatted
  • ๐Ÿ—๏ธ Builder Patterns: Fluent APIs for creating complex XML structures
  • ๐ŸŒ Namespace Support: Comprehensive namespace handling and resolution
  • โš™๏ธ Configurable: Multiple serialization options and presets
  • ๐Ÿงช Well Tested: 100% test coverage with 59 passing tests

Quick Example

// Parse XML while preserving all formatting
Document doc = Document.of(
        """
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Project configuration -->
    <project xmlns="http://maven.apache.org/POM/4.0.0">
        <groupId>com.example</groupId>
        <version>1.0.0</version>
    </project>
    """);
Editor editor = new Editor(doc);

// Make targeted changes
Element version = editor.root().descendant("version").orElseThrow();
editor.setTextContent(version, "2.0.0");

// Output preserves ALL original formatting
String result = editor.toXml();
// Comments, whitespace, namespaces - everything preserved
// Only the version number changes

Output:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Project configuration -->
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <groupId>com.example</groupId>
    <version>1.0.1</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
    </dependencies>
</project>

Notice how:

  • Original XML declaration and comments are preserved
  • Existing formatting remains unchanged
  • New elements follow the inferred indentation pattern
  • Only the modified version element changed

When to Use DomTrip

DomTrip is perfect for scenarios where you need to:

  • Edit configuration files (Maven POMs, Spring configs, etc.)
  • Modify XML documents while preserving their original structure
  • Build XML transformation tools that maintain formatting
  • Create XML editors with lossless round-trip capabilities
  • Process SOAP/XML messages without losing formatting details

Getting Started

Ready to try DomTrip? Head over to the Installation Guide to get started in minutes!