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.

Answer from Voo on Stack Overflow
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/

🌐
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 ...
Discussions

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 XML parser for Java - Stack Overflow
I need to read smallish (few MB at the most, UTF-8 encoded) XML files, rummage around looking at various elements and attributes, perhaps modify a few and write the XML back out again to disk (pref... More on stackoverflow.com
🌐 stackoverflow.com
What is the best way to parse and munipulate large XML files in Java
As stated in another comment....sax is probably the best place to start assuming the XML file you have is just something you have created. If the XML you are importing has a schema associated with it...you could also use JAXB http://www.oracle.com/technetwork/articles/javase/index-140168.html . This allows you to generate java classes to work with your XML file. Regarding the best way to structure a java project...check out Maven. Its a great tool for creating projects with a well known structure. More on reddit.com
🌐 r/java
14
4
July 8, 2012
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
February 21, 2014
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › java_xml › java_dom_parse_document.htm
Java DOM Parser - Parse XML Document
Java DOM parser is a Java API to parse any XML document. Using the methods provided, we can retrieve root element, sub elements and their attributes using Java DOM parser. In this tutorial we have used the getTagName() method to retrieve the tag
🌐
GitHub
github.com › RameshMF › java-xml-tutorial
GitHub - RameshMF/java-xml-tutorial: Tutorial to parse or processing xml file in Java with different XML Parsers · GitHub
Here are few examples to show how to create, modify and read an XML file with Java DOM, SAX, JDOM. DOM Parser is the easiest Java XML parser to learn. DOM parser loads the XML file into memory and we can traverse it node by node to parse the XML.
Starred by 4 users
Forked by 3 users
Find elsewhere
🌐
Baeldung
baeldung.com › home › xml › working with xml files in java using dom parsing
Working with XML Files in Java Using DOM Parsing
June 20, 2025 - In this quick article, we learned how to use the Xerces DOM parser to create, modify and save an XML document.
🌐
Inductive Automation
docs.inductiveautomation.com › ignition platform › scripting › scripting examples › parsing xml with java libraries
Parsing XML with Java Libraries | Ignition User Manual
There are several ways to import XML data using the DOM parser, depending on how it's stored. It can retrieve data from an XML file using the file path or from a string. Regardless of the method, it provides a root object representing the XML document. ... from javax.xml.parsers import ...
🌐
Quill
quilljs.com
Quill - Your powerful rich text editor
Quill is a free, open source rich text editor built for the modern web.
🌐
W3Schools
w3schools.com › xml › xml_parser.asp
XML Parser
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
TutorialsPoint
tutorialspoint.com › home › java_xml › java xml parsers
Java XML Parsers
September 1, 2008 - Explore the different types of Java XML parsers, including DOM, SAX, and StAX, to effectively manipulate and process XML data in your applications.
🌐
Oracle
docs.oracle.com › cd › E14571_01 › appdev.1111 › b28394 › adx_j_parser.htm
XML Parsing for Java
The XML parser for Java can parse unqualified element types and attribute names as well as those in namespaces. Namespaces are a mechanism to resolve or avoid name collisions between element types or attributes in XML documents by providing "universal" names.
🌐
Semgrep
semgrep.dev › blog › 2023 › xml-security-in-java
XML Security in Java | Semgrep
Vasilii and I created attack payloads for each of the 10 ways to include external content into XML documents. We tested 10 classes and there are 16 security features to test, resulting in 160 parser configurations. To test these configurations, we tried to parse each of the 10 payloads and verified whether or not external requests were made. Thus, we ran 1600 tests! The full table of results can be found in our · Java XXE Cheatsheet.
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

🌐
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
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. - rkalla/simple-java-xml-parser
Starred by 38 users
Forked by 20 users
Languages   Java
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › javax › xml › parsers › package-summary.html
javax.xml.parsers (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... Provides classes allowing the processing of XML documents. ... Provides classes allowing the processing of XML documents. Two types of plugable parsers are supported:
🌐
DevGenius
blog.devgenius.io › xml-parsing-using-jaxb-de5529b91fbe
XML Parsing using JAXB. In this article, we will learn about… | by Kruti Dave | Dev Genius
November 1, 2023 - It is also known as the Simple API for XML. It can be consider as an event based parser as it does not store anything into memory and it start parsing at the beginning of the document in a sequence manner. Since SAX is more memory-efficient than DOM as it is based on events and works on callback mechanism. ... JAXB — Java Architecture for XML Binding — is used to convert objects from/to XML.
🌐
Prettier
prettier.io
Prettier · Opinionated Code Formatter · Prettier
Java · PHP · Ruby · Rust · TOML · XML · And more... prettier-js prettier.el Apheleia · espresso-prettier · Prettier Prettier⁺ · JsPrettier · vim-prettier neoformat ALE coc-prettier · JavaScriptPrettier · prettier-vscode · Built-in support · Got more?Send a PR ·
🌐
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 - Whenever a character is found in an XML document, the char() will be executed. That’s why we append() the string to keep this data. So the JDOM parser is a combination of the DOM and SAX parsers that we have already seen. It’s an open-source Java-based library API.
Address   TIDEL Park, 305, 3rd Floor, D-North, 4, Rajiv Gandhi Salai, Tharamani,, 600113, Chennai