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:
- DOM - in-memory tree model with full random access.
- SAX - event-driven, push-based streaming.
- StAX - cursor-based, pull streaming.
- VTD-XML - non-extractive, offset-based parsing.
- Expat - the C streaming parser library.
- Pull Parsing - the Iterator-style parsing pattern.
- Xerces - the Apache DOM/SAX parser family.
- MSXML - Microsoft's Windows XML services.
- libxml2 - the C library behind many language bindings.