DOM and Streaming Parsers

In-Memory and Streaming Parser APIs

XML libraries offer three programming models: a tree you navigate (DOM), events pushed to callbacks (SAX), or a cursor you pull forward (StAX and other pull parsers). The JDK has all three, so these examples are Java 21 programs run as single source files, each adding up BookNest's prices.

Subsections


Parsers and Programs

An XML parser is a library or API that reads raw XML text, checks it for well-formedness (and, optionally, validity against a schema), and exposes its content and structure to an application. Parsers differ mainly in their processing model: whether the whole document is built into memory as a navigable tree, or the document is streamed to the application piece by piece.

This chapter surveys the parsing models and parser implementations commonly encountered when working with XML: