Examples to get you started

This page lists some example code that may get you started.

DOM content retrieval and modification

DOMUtils class contains some potentionally useful methods. You should consider using the getLocalName() and getXmlns...() methods at least - they implements flawed DOM Level 1/2 methods correctly.

To obtain a list of all nodes on the ascendant-or-self axis just use

List<Node> ascendantOrSelf = DOMUtils.getNodePathToRoot(node);

For debugging purposes you may print nodes on this axis:

String pathToRoot = DOMUtils.getPathToRoot(node);

To get a string description of the node type:

String nodeType = DOMUtils.getNodeType(Node.ELEMENT_NODE);
// nodeType is "ELEMENT_NODE"

To modify a DOM document use the DOMMutils (DOM Mutation Utilities) class. For example, to insert an element after fifth character of a text node (thus splitting this text node) use:

DOMMutils.insertNode(element, DomPointerFactory.create(textNode, 5), false);

Utilities for working with the DOM events

There are two enums you may use to work with DOM events: EventClassEnum containing four enum classes: UI, Mouse, Mutation and HTML events, and EventTypesEnum providing all events and their description. Currently only Mutation events are enlisted in the enum, I'll add another events when requested by the community.

Copying between various TrAX sources and targets, converting TrAX sources

Some useful methods are provided by the SourceEnum enum: to convert a SAX source into a DOM source just use:

DOMSource ds = SourceEnum.DOM.convert(saxSource, null);

To copy from a Source to a Result just use:

SourceEnum.copy(SourceEnum.STAX.convert(domSource, null), staxResult);

STAX support

A STAXSource extending Source and a STAXResult extending Result are provided, along with some utilities to read from/construct a DOM node using STAX events.

Node wrappers for inserting the nodes into a collection

The NodeListAdapter class wraps the NamedNodeMap interface and provides the NodeList interface. You may obtain all nodes contained in a NodeList interface using

List<Node> nodes = DOMUtils.nodeListToList(node.getChildNodes());

Piped input/output streams with exception transferring

You may use PipedExceptionInputStream to connect two threads using a pipe. If the producer thread fails, the exception is trasferred to the receiver, causing the receiver to fail in any read() method with the producer's exception. You may use IOWithCauseException exception to wrap any exception in the IOException.

Splitting a DOM document into a set of nametrees

SAX utilities

Use ElementStack instance to verify in ContentHandler if elements are properly nested. You may use SAXDebuggerHandler and/or SAXDebuggerReader to print SAX events in a SAX chain. To wrap an exception in the SAXException use SAXExceptionFixed which sets the cause exception correctly.

To setup a parser you may use constants in SAXConstants class. Please note that not all parsers supports all these constants - this list was taken from the Xerces's features and properties list. The class currently does not contain properties yet.

Entity utilities

A mapper between a DOM document and its image with nodes coalesced and entities resolved

DOM range utilities

Node identification mechanism independent of the DTD/schema

The IDManager class allows you to create and/or obtain ID for each node in the document. It may be registered as an observer to capture changes in the document and remove/add id attributes to nodes being removed/added.