XOM XML Library: Strict Correctness by Design

The XOM (XML Object Model) library is an open-source, tree-based Java API designed by Elliotte Rusty Harold to provide simple, robust, and safe XML processing. Unlike traditional APIs such as the standard W3C Document Object Model (DOM), which often allow developers to construct illegal XML representations in memory, XOM guarantees that an invalid XML document can never be created through its API. This article explains what XOM is, how it differs from traditional XML models, and the architectural principles it employs to enforce strict XML correctness and structural invariants by design.


What Is XOM?

XOM is a lightweight, high-performance Java library for parsing, building, and manipulating XML. While it shares conceptual similarities with DOM and JDOM—representing XML documents as hierarchical, in-memory tree structures—its primary differentiator is an uncompromising focus on correctness and predictability.

In standard DOM implementations, memory structures can easily drift into states that violate the XML 1.0 specification, such as elements containing illegal characters or unmapped namespace prefixes. These errors are often only discovered downstream during serialization. XOM eliminates this category of bugs by rejecting invalid data the moment it is introduced.


Structural Invariants Enforced by Design

XOM enforces strict structural rules throughout the lifecycle of an XML node. These invariants prevent the internal tree model from ever entering an inconsistent state:


Namespace and Naming Correctness

Namespace handling is one of the most common sources of subtle bugs in XML processing. XOM enforces strict namespace and naming invariants on creation and modification:


Character Data and Content Integrity

XOM actively validates character data to prevent the insertion of characters that are illegal under the XML 1.0 specification:


The Fail-Fast Philosophy

The core design pattern of XOM is fail-fast validation via unchecked exceptions. Instead of relying on boolean return values or delayed validation during serialization, XOM constructors and mutation methods immediately throw specific runtime exceptions (subclasses of XMLException, such as IllegalDataException or NoSuchChildException) when an operation would violate XML rules.

By eliminating silent failures and invalid intermediate states, XOM allows developers to trust the integrity of any Document or Element instance passed across application boundaries. If an XOM object exists in memory, it is guaranteed to represent valid, well-formed XML.