Working with XML in Java: DOM, SAX, StAX and JAXB
XML (eXtensible Markup Language) remains one of the key formats for storing and transmitting structured data. In Java, there are several built-in and popular libraries for working with XML. In this article, we will cover all the main approaches: from classic DOM and SAX to modern JAXB.
1. DOM (Document Object Model) — Full Access to the Document
The DOM parser loads the entire XML document into memory as a tree structure. This provides full control over the document: you can read, modify, delete, and add nodes.
Advantages of DOM:
- Random access to any element of the document
- Ability to modify the XML structure
- Intuitive object model
Disadvantages:
- High memory consumption (entire document in RAM)
- Slow performance with large files
Example of using DOM:
import javax.xml.parsers.*;import org.w3c.dom.*;import java.io.*;
public class DomExample { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("books.xml"));
NodeList bookList = document.getElementsByTagName("book"); for (int i = 0; i < bookList.getLength(); i++) { Element book = (Element) bookList.item(i); String title = book.getElementsByTagName("title").item(0).getTextContent(); String author = book.getElementsByTagName("author").item(0).getTextContent(); System.out.println(title + " — " + author); } }}2. SAX (Simple API for XML) — Streaming Parsing
SAX is an event-driven parser. It reads XML sequentially and calls callback methods when encountering opening/closing tags, text, or attributes. Memory consumption is minimal.
When to use SAX:
- Huge XML files (hundreds of megabytes)
- Read-only, no modification needed
- Limited device resources
Example of a SAX parser:
import org.xml.sax.*;import org.xml.sax.helpers.*;import javax.xml.parsers.*;
public class SaxExample { public static void main(String[] args) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() { boolean isTitle = false;
public void startElement(String uri, String localName, String qName, Attributes attributes) { if (qName.equalsIgnoreCase("title")) isTitle = true; }
public void characters(char ch[], int start, int length) { if (isTitle) { System.out.println("Title: " + new String(ch, start, length)); isTitle = false; } } };
saxParser.parse("books.xml", handler); }}3. StAX (Streaming API for XML) — Convenient Streaming Parsing
StAX is a compromise between DOM and SAX. It also works in streaming mode but provides a more convenient pull interface (you request the next element yourself).
Two ways to work with StAX:
- XMLStreamReader — for reading
- XMLStreamWriter — for writing
import javax.xml.stream.*;import java.io.*;
public class StaxExample { public static void main(String[] args) throws Exception { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createXMLStreamReade Also in library
Swagger/OpenAPI generation for TypeScript: best tools
Working with CSV in Go (Golang): A Complete Guide to encoding/csv
OpenAI SDK for JavaScript (Node.js): Complete Guide with Examples
Authorization and Authentication in PHP: A Complete Guide with Examples