Working with XML in Java: DOM, SAX, StAX, and JAXB

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

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 ways to work with XML, each suitable for different tasks: from reading small configurations to processing gigabyte-sized files. In this article, we will cover four main approaches: DOM, SAX, StAX, and JAXB.



1. DOM (Document Object Model) — Full Loading into Memory



The DOM parser loads the entire XML document into memory, creating a tree structure. This is convenient for navigation and modification, but inefficient for large files.



Example of reading XML using DOM:



import javax.xml.parsers.*;import org.w3c.dom.*;

public class DomExample { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("books.xml");

NodeList bookList = doc.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("Book: " + title + ", author: " + author); } }}


  • Pros: convenient access to any node, XPath support, ability to modify the document.
  • Cons: requires a lot of memory, slow for large files.
  • When to use: configuration files, small documents (up to a few MB).


2. SAX (Simple API for XML) — Event-Driven Streaming Parser



SAX reads XML sequentially and generates events (start element, text, end element). You process only the events you need — this allows you to work with files of any size.



Example of processing XML using SAX:



import org.xml.sax.*;import org.xml.sax.helpers.*;import javax.xml.parsers.*;

public class SaxExample extends DefaultHandler { private boolean inTitle = false;

public void startElement(String uri, String localName, String qName, Attributes attrs) { if (qName.equals("title")) inTitle = true; }

public void characters(char[] ch, int start, int length) { if (inTitle) System.out.println("Title: " + new String(ch, start, length)); }

public void endElement(String uri, String localName, String qName) { if (qName.equals("title")) inTitle = false; }

public static void main(String[] args) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); parser.parse("books.xml", new SaxExample()); }}


  • Pros: minimal memory consumption, high speed.
  • Cons: cannot go back to a previous element, difficult to modify the document.
  • When to use: large XML files (logs, database dumps, reports).


3. StAX (Streaming API for XML) — Streaming Parser with Cursor Control



StAX is a more modern alternative to SAX. You control the reading yourself by moving a cursor through the XML stream. Two modes are available: Cursor API (event iterator) and Event API (event processing).



Example with Cursor API (XMLStreamReader):



import javax.xml.stream.*;

public class StaxExample { public static void main(String[] args) throws Exceptio

Recommendations