Fundamentals 20 min read

XML Parsing in Java: DOM, SAX, JDOM, and DOM4J Comparison and Examples

This article provides a comprehensive guide to parsing XML in Java, covering the official DOM and SAX methods, third‑party JDOM and DOM4J libraries, detailed code examples for each approach, and a performance and feature comparison to help developers choose the most suitable parser.

Java Captain
Java Captain
Java Captain
XML Parsing in Java: DOM, SAX, JDOM, and DOM4J Comparison and Examples

XML is widely used for data storage and transmission, and Java offers several ways to parse and generate XML files for cross‑platform communication.

1. DOM Parsing – Uses the standard Java API (DocumentBuilderFactory, DocumentBuilder) to load the entire XML document into memory, allowing traversal of nodes, attributes, and child elements. Sample code demonstrates creating a DocumentBuilder, parsing books.xml , iterating over book nodes, and printing attribute names and values.

package com.study.domtest;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class DOMTest {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse("books.xml");
        NodeList bookList = document.getElementsByTagName("book");
        System.out.println("Total books: " + bookList.getLength());
        // iterate and print attributes and child nodes ...
    }
}

2. SAX Parsing – An event‑driven, low‑memory approach using DefaultHandler . The article shows a SAXParserHandler that captures start/end of elements, attributes, and character data, building Book objects on the fly. A test class demonstrates parsing and printing the resulting list.

package com.study.saxtest.handler;
import org.xml.sax.helpers.DefaultHandler;
public class SAXParserHandler extends DefaultHandler {
    private List
bookList = new ArrayList<>();
    private Book book;
    private String value;
    // override startElement, endElement, characters ...
}

3. JDOM Parsing – A third‑party library (requires jdom‑2.0.5.jar ) that provides a more Java‑friendly API. The example creates a SAXBuilder , reads the XML with an InputStreamReader , and iterates over elements and attributes to populate Book objects.

package com.study.jdomtest1.test;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
public class JDOMTest {
    public static void main(String[] args) throws Exception {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(new FileInputStream("books.xml"));
        Element root = doc.getRootElement();
        for (Element book : root.getChildren()) {
            // process attributes and child elements ...
        }
    }
}

4. DOM4J Parsing – Another third‑party library (requires dom4j‑1.6.1.jar ). Using SAXReader , the code reads the XML file, iterates over book elements, prints attributes, and traverses child nodes.

package com.study.dom4jtest;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
public class DOM4JTest {
    public static void main(String[] args) throws Exception {
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File("books.xml"));
        Element root = doc.getRootElement();
        for (Iterator
it = root.elementIterator(); it.hasNext(); ) {
            Element book = it.next();
            // print attributes and child elements ...
        }
    }
}

5. Comparison and Performance – The article summarizes the strengths and weaknesses of each method. DOM loads the whole document (high memory usage), SAX is fastest and memory‑efficient, JDOM and DOM4J offer more flexible APIs with moderate performance. Benchmark results (few‑KB XML) show typical speeds: SAX ≈ 6 ms, DOM ≈ 33 ms, DOM4J ≈ 45 ms, JDOM ≈ 69 ms. Recommendations suggest using DOM4J for larger or more complex XML, while SAX is preferred for speed‑critical scenarios.

Additional notes cover how to add required JAR files to a project (via Build Path or a lib folder) and provide QR‑code contact information for the author’s WeChat “Java团长”.

JavaparsingXMLDOMDOM4JJDOMSAX
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.