I think you should not consider any specific parser implementation. Java API for XML Processing lets you use any conforming parser implementation in a standard way. The code should be much more portable, and when you realise that a specific parser has grown too old, you can replace it with another without changing a line of your code (if you do it correctly).

Basically there are three ways of handling XML in a standard way:

  • SAX This is the simplest API. You read the XML by defining a Handler class that receives the data inside elements/attributes when the XML gets processed in a serial way. It is faster and simpler if you only plan to read some attributes/elements and/or write some values back (your case).
  • DOM This method creates an object tree which lets you modify/access it randomly so it is better for complex XML manipulation and handling.
  • StAX This is in the middle of the path between SAX and DOM. You just write code to pull the data from the parser you are interested in when it is processed.

Forget about proprietary APIs such as JDOM or Apache ones (i.e. Apache Xerces XMLSerializer) because will tie you to a specific implementation that can evolve in time or lose backwards compatibility, which will make you change your code in the future when you want to upgrade to a new version of JDOM or whatever parser you use. If you stick to Java standard API (using factories and interfaces) your code will be much more modular and maintainable.

There is no need to say that all (I haven't checked all, but I'm almost sure) of the parsers proposed comply with a JAXP implementation so technically you can use all, no matter which.

Answer from Fernando Miguélez on Stack Overflow
Top answer
1 of 8
262

I think you should not consider any specific parser implementation. Java API for XML Processing lets you use any conforming parser implementation in a standard way. The code should be much more portable, and when you realise that a specific parser has grown too old, you can replace it with another without changing a line of your code (if you do it correctly).

Basically there are three ways of handling XML in a standard way:

  • SAX This is the simplest API. You read the XML by defining a Handler class that receives the data inside elements/attributes when the XML gets processed in a serial way. It is faster and simpler if you only plan to read some attributes/elements and/or write some values back (your case).
  • DOM This method creates an object tree which lets you modify/access it randomly so it is better for complex XML manipulation and handling.
  • StAX This is in the middle of the path between SAX and DOM. You just write code to pull the data from the parser you are interested in when it is processed.

Forget about proprietary APIs such as JDOM or Apache ones (i.e. Apache Xerces XMLSerializer) because will tie you to a specific implementation that can evolve in time or lose backwards compatibility, which will make you change your code in the future when you want to upgrade to a new version of JDOM or whatever parser you use. If you stick to Java standard API (using factories and interfaces) your code will be much more modular and maintainable.

There is no need to say that all (I haven't checked all, but I'm almost sure) of the parsers proposed comply with a JAXP implementation so technically you can use all, no matter which.

2 of 8
132

Here is a nice comparision on DOM, SAX, StAX & TrAX (Source: http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/1.6/tutorial/doc/SJSXP2.html )

Feature                  StAX                  SAX                      DOM                  TrAX

API Type                Pull,streaming     Push,streaming    In memory tree    XSLT Rule

Ease of Use          High                    Medium                 High                    Medium

XPath Capability   No                       No                        Yes                      Yes

CPU & Memory     Good                  Good                    Varies                  Varies

Forward Only        Yes                    Yes                        No                       No

Read XML              Yes                    Yes                        Yes                     Yes

Write XML              Yes                    No                          Yes                     Yes

CRUD                      No                      No                         Yes                     No

🌐
Baeldung
baeldung.com › home › xml › xml libraries support in java
XML Libraries Support in Java
June 20, 2025 - In Java XML support we can find few API definitions, each one has its pros and cons. • SAX: It is an event based parsing API, it provides a low level access, is memory efficient and faster than DOM since it doesn’t load the whole document tree in memory but it doesn’t provide support for navigation like the one provided by XPath, although it is more efficient it is harder to use too. • DOM: It as model based parser that loads a tree structure document in memory, so we have the original elements order, we can navigate our document both directions, it provides an API for reading and writing, it offers XML manipulation and it is very easy to use although the price is high strain on memory resources.
Discussions

Is there an easier way to parse XML in Java? - Stack Overflow
Take a look at http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ for how to use the DOM parser. DOM will create a tree which you can navigate pretty easily. SAX is best for large documents but DOM is much easier if slower and much more memory intensive. More on stackoverflow.com
🌐 stackoverflow.com
What are some well maintained XML libraries?
JAXB. Jackson. If support for either of those is dropped then a large percentage of systems are screwed. More on reddit.com
🌐 r/java
25
25
April 8, 2024
Best Java JSON Parser: Gson or Jackson?

Jackson, but mostly because I tend to use it from Spring MVC and it just works without any extra effort.

More on reddit.com
🌐 r/java
61
47
April 27, 2015
Which Java XML parser should I choose ?

what do you consider medium sized? I would say likely a SAX parser since you only need a few of the values. DOM parser will be eaiser to use and the code will likely be smaller but it requires processing the whole file and building an object hierarchy in memory, so will be slower and use more memory.

More on reddit.com
🌐 r/javahelp
2
2
January 18, 2020
Top answer
1 of 7
238

Actually Java supports 4 methods to parse XML out of the box:

DOM Parser/Builder: The whole XML structure is loaded into memory and you can use the well known DOM methods to work with it. DOM also allows you to write to the document with Xslt transformations. Example:

Copypublic static void parse() throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    File file = new File("test.xml");
    Document doc = builder.parse(file);
    // Do something with the document here.
}

SAX Parser: Solely to read a XML document. The Sax parser runs through the document and calls callback methods of the user. There are methods for start/end of a document, element and so on. They're defined in org.xml.sax.ContentHandler and there's an empty helper class DefaultHandler.

Copypublic static void parse() throws ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    SAXParser saxParser = factory.newSAXParser();
    File file = new File("test.xml");
    saxParser.parse(file, new ElementHandler());    // specify handler
}

StAx Reader/Writer: This works with a datastream oriented interface. The program asks for the next element when it's ready just like a cursor/iterator. You can also create documents with it. Read document:

Copypublic static void parse() throws XMLStreamException, IOException {
    try (FileInputStream fis = new FileInputStream("test.xml")) {
        XMLInputFactory xmlInFact = XMLInputFactory.newInstance();
        XMLStreamReader reader = xmlInFact.createXMLStreamReader(fis);
        while(reader.hasNext()) {
            reader.next(); // do something here
        }
    }
}

Write document:

Copypublic static void parse() throws XMLStreamException, IOException {
    try (FileOutputStream fos = new FileOutputStream("test.xml")){
        XMLOutputFactory xmlOutFact = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = xmlOutFact.createXMLStreamWriter(fos);
        writer.writeStartDocument();
        writer.writeStartElement("test");
        // write stuff
        writer.writeEndElement();
    }
}

JAXB: The newest implementation to read XML documents: Is part of Java 6 in v2. This allows us to serialize java objects from a document. You read the document with a class that implements a interface to javax.xml.bind.Unmarshaller (you get a class for this from JAXBContext.newInstance). The context has to be initialized with the used classes, but you just have to specify the root classes and don't have to worry about static referenced classes. You use annotations to specify which classes should be elements (@XmlRootElement) and which fields are elements(@XmlElement) or attributes (@XmlAttribute, what a surprise!)

Copypublic static void parse() throws JAXBException, IOException {
    try (FileInputStream adrFile = new FileInputStream("test")) {
        JAXBContext ctx = JAXBContext.newInstance(RootElementClass.class);
        Unmarshaller um = ctx.createUnmarshaller();
        RootElementClass rootElement = (RootElementClass) um.unmarshal(adrFile);
    }
}

Write document:

Copypublic static void parse(RootElementClass out) throws IOException, JAXBException {
    try (FileOutputStream adrFile = new FileOutputStream("test.xml")) {
        JAXBContext ctx = JAXBContext.newInstance(RootElementClass.class);
        Marshaller ma = ctx.createMarshaller();
        ma.marshal(out, adrFile);
    }
}

Examples shamelessly copied from some old lecture slides ;-)

Edit: About "which API should I use?". Well it depends - not all APIs have the same capabilities as you see, but if you have control over the classes you use to map the XML document JAXB is my personal favorite, really elegant and simple solution (though I haven't used it for really large documents, it could get a bit complex). SAX is pretty easy to use too and just stay away from DOM if you don't have a really good reason to use it - old, clunky API in my opinion. I don't think there are any modern 3rd party libraries that feature anything especially useful that's missing from the STL and the standard libraries have the usual advantages of being extremely well tested, documented and stable.

2 of 7
13

Java supports two methods for XML parsing out of the box.

SAXParser

You can use this parser if you want to parse large XML files and/or don't want to use a lot of memory.

http://download.oracle.com/javase/6/docs/api/javax/xml/parsers/SAXParserFactory.html

Example: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

DOMParser

You can use this parser if you need to do XPath queries or need to have the complete DOM available.

http://download.oracle.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilderFactory.html

Example: http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

🌐
Baeldung
baeldung.com › home › series › a guide to xml in java
A Guide to XML in Java | Baeldung
September 28, 2023 - A SAX parser is an event-based parser – it parses the XML document using callbacks without loading the whole document into memory. ... A StAX Parser is median between DOM and SAX parser.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-xml-parser
Java XML Parser | DigitalOcean
August 4, 2022 - JDOM provides a great Java XML parser API to read, edit and write XML documents easily. JDOM provides wrapper classes to chose your underlying implementation from SAX Parser, DOM Parser, STAX Event Parser and STAX Stream Parser.
🌐
TutorialsPoint
tutorialspoint.com › home › java_xml › java xml parsers
Java XML Parsers
September 1, 2008 - Learn about various Java XML parsers such as DOM, SAX, and StAX for efficient XML data handling in your Java applications.
🌐
Medium
medium.com › tech-travelstart › efficient-xml-parsing-with-java-df3169e1766b
Efficient XML Parsing with Java. For a language that likes XML so much… | by Travelstart Admin | Tech@Travelstart | Medium
March 5, 2018 - The DOM method is fine for small documents like configuration files but completely falls apart when working with documents of any real size. When it comes to stream parsing in Java the SAX parser seems to be the most common choice.
🌐
Edureka Community
edureka.co › home › community › categories › java › best xml parser for java
Best XML parser for Java | Edureka Community
July 20, 2018 - We need to read smallish (few MB at the most, UTF-8 encoded) XML files, rummage around looking at ... Woodstox XOM dom4j VTD-XML Xerces-J Crimson
Find elsewhere
🌐
GitHub
github.com › FasterXML › aalto-xml
GitHub - FasterXML/aalto-xml: Ultra-high performance non-blocking XML processor (Stax API + extensions)
Stax (javax.xml.stream) interface -- countless tutorials exist. ... Non-blocking parsing interface is extension of basic Stax (and Stax2) API, with extensions defined in 'com.fasterxml.aalto' package: AsyncXMLInputFactory offers factory methods for creating non-blocking parsers
Starred by 304 users
Forked by 73 users
Languages   Java 100.0% | Java 100.0%
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-xml-parsers-1
Java XML Parsers - GeeksforGeeks
June 27, 2024 - The DOM parser is good when working with an XML in-memory setup; the SAX parser works well within a low-memory, high-performance environment; and the StAX parser, appropriate for a good balance between the two, will keep you in control of the ...
🌐
Quora
quora.com › What-is-the-best-way-to-parse-XML-in-Java-without-external-libraries
What is the best way to parse XML in Java without external libraries? - Quora
Answer (1 of 5): Given that these libraries exist, are robust and and handle all the zillions of icky details of XML, doing it “yourself” is probably not a good idea; it will take you much longer than you think to produce fully compliant XML readers. If you are willing to give up on full XML com...
🌐
Oracle
docs.oracle.com › cd › B12037_01 › appdev.101 › b10794 › adx04paj.htm
3 XML Parser for Java
If you are storing documents that ... storage and searching techniques with lots of examples. Are there plans to add an ambiguous content mode to the XDK Parser for Java?...
🌐
Codoid
codoid.com › automation-testing › read-data-from-xml-by-using-different-parsers-in-java
Read Data from XML by Using Different Parsers in Java - Codoid
September 18, 2024 - So once the XML document has been read, we would reuse the document and the XPath object in all the methods. We hope you have found the parser that fits your requirement and in-process also enjoyed reading this article. So to sum things up, we have seen how each parser works to understand the pros and cons of each type. Choosing the apt parser might seem like a very small aspect when compared to the entire scale of the project. But as one of the best software testing service providers, we believe in attaining maximum efficiency in each process, be it small or big.
Address   TIDEL Park, 305, 3rd Floor, D-North, 4, Rajiv Gandhi Salai, Tharamani,, 600113, Chennai
🌐
Medium
sekarbala.medium.com › java-xml-parsers-c5467e2dc2e1
Java XML Parsers. In Java, there are several types of XML… | by Balasekar Natarajan | Medium
November 2, 2024 - JAXB: Best for mapping XML data to Java objects. Each parser has its advantages and disadvantages depending on the use case and size of the XML document.
🌐
Java-source
java-source.net › open-source › xml-parsers
Open Source XML Parsers in Java
Go To Piccolo XML Parser · Go To JiBX: Binding XML to Java Code
🌐
Coderanch
coderanch.com › t › 624791 › languages › Library-XML-parsing
Best Library for XML parsing (XML forum at Coderanch)
If you intend to make use of just a subset of the XML, then I wouldn't recommend either DOM or JAXB. Besides XPath, other alternatives that are also part of the JRE are the SAX and StAX APIs. Plus, there are 3rd party libraries like XOM, JDOM and dom4j, but if the aim is merely to read, then I probably wouldn't use those. ... Without any more details, I am going to suggest something simple. Use the DOM parser and XPATH - XPATH needs a DOM to work with so your first step will be learning how to get a DOM into memory.
🌐
GitHub
github.com › rkalla › simple-java-xml-parser
GitHub - rkalla/simple-java-xml-parser: Java XML parser with XPath's ease-of-use and pull-parsing performance. Built for use on Android, web services, web applications or client-side Java. · GitHub
Description ----------- A very small (4 classes) abstraction layer that sits on top of the XML Pull Parser spec (http://xmlpull.org/) to provide a namespace-capable XML Parser with the ease-of-use of XPath with the performance of a native pull parser. SJXP was designed to work on any Java platform, including Android 1.5+. This library supports parsing 2 types of data from an XML source: * Character data (text between tags) * Attribute values Parsing rules (IRule instances) are defined as a series of XPath-esque "location paths" pointing at elements or attribute names, for example the following
Starred by 38 users
Forked by 20 users
Languages   Java