Working with XML in Java: Libraries and Tools for Parsing

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: Overview of Libraries and Tools

XML (eXtensible Markup Language) remains one of the key formats for storing and transmitting structured data in enterprise applications, web services, and configuration files. Java provides a rich set of built-in and third-party libraries for working with XML. In this article, we will thoroughly examine the main tools, their features, and usage examples.



Built-in APIs for Working with XML

The standard Java library (Java SE) includes three main APIs for parsing and processing XML:



1. DOM (Document Object Model)

DOM is an API that loads the entire XML document into memory as a tree structure. This is convenient for random access to elements but requires significant resources when working with large files.

When to use: when you need to frequently modify the document or perform complex queries on its structure.



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("data.xml");

NodeList books = doc.getElementsByTagName("book"); for (int i = 0; i < books.getLength(); i++) { Element book = (Element) books.item(i); String title = book.getElementsByTagName("title").item(0).getTextContent(); System.out.println("Book: " + title); } }}


2. SAX (Simple API for XML)

SAX is an event-driven parser. It reads the document sequentially and calls handler methods when encountering tags, attributes, or text. It does not create a tree in memory, making it ideal for large files.

When to use: for streaming processing of huge XML files (e.g., logs or export data).



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

public class SaxExample extends DefaultHandler { public void startElement(String uri, String localName, String qName, Attributes attrs) { if ("book".equals(qName)) { System.out.println("Found book with id: " + attrs.getValue("id")); } }

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


3. StAX (Streaming API for XML)

StAX is a streaming pull parser. Unlike SAX, where events are "pushed" to the handler, StAX allows the program to request the next element (pull model). This provides more control over the parsing process.

When to use: when you need the flexibility of SAX but with the ability to pause and resume reading.



import javax.xml.stream.*;

public class StaxExample { public static void main(String[] args) throws Exception { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createXMLStreamReader( new java.io.FileInputStream("data.xml"));

while (reader.hasNext()) { int event = reader.next(); if (event == XMLStreamConstants.START_ELEMENT && "title".equals(reader.getLocalName())) { System.out.println("Title: " + reader.getElementText()); } } reader.close(); }}


Binding XML to Java Objects: JAXB

JAXB (Java Architecture for XML Binding) is a standard API for converting XML to Java objects and back (marshalling/unmarshalling). By

Recommendations