A Guide to XML in Java
Last modified: January 21, 2021
1. Overview
This is a guide to working with XML in Java.
We'll go over the most common Java XML processing libraries – for both parsing and binding.
2. DOM Parsers
Simply put, a DOM parser works on the entire XML document, loads it into memory and constructs a tree representation of the document.
Useful Resources
- How to read XML file in Java – (DOM Parser)
- Java DOM Parser – Parse XML Document
- Java XML DOM Parser Example Tutorial
- Easy DOM Parsing in Java
3. SAX Parser
A SAX parser is an event-based parser – it parses the XML document using callbacks without loading the whole document into memory.
Useful Resources
- How to read XML file in Java – (SAX Parser)
- Java SAX Parser – Parse XML Document
- How to parse an xml using SAX parser and DefaultHandler
- XML parsing using SaxParser
- Java SAX Parser Example Tutorial to parse XML to List of Objects
4. StAX Parser
A StAX Parser is median between DOM and SAX parser.
Useful Resources
5. JAXB
JAXB – Java Architecture for XML Binding – is used to convert objects from/to XML.
JAXB is a part of the Java SE platform and one of the APIs in Jakarta EE.
Useful Resources
- Oracle JAXB Tutorial
- JAXB Hello World Example
- JAXB Tutorial for Java XML Binding – The ULTIMATE Guide
- JAXB Tutorial
- JAXB Marshal Example
- JAXB Unmarshal Example
- How to convert Java Object to XML – JAXB Example
- JAXB example: Marshalling and Unmarshalling HashMap in java
- JAXB exmaple: Marshalling and Unmarshalling list or set of objects
- JAXB Annotations
- JAXB and Namespace Prefixes
- JAXB and Inheritance – Using XmlAdapter
6. XStream
XStream is a simple library to serialize objects to/from XML.
Here's the maven dependency to use to get it into a Maven enabled project:
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.8</version>
</dependency>
Useful Resources
- Official Website
- Github
- Javadoc
- Two Minute Tutorial
- XStream – XStreamely Easy Way to Work With XML Data in Java
- XStream – Java to XML and Back
7. Jackson XML
Jackson XML is an extension of Jackson JSON processor for reading and writing XML encoded data.
In order to use it – here's the simple Maven dependency you'll need:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
Note: Latest version of jackson-dataformat-xml right now is 2.6.3.
Useful Resources
8. Simple XML
Simple XML is a high performance XML serialization framework for Java.
In order to use it you need the following dependency:
<dependency>
<groupId>org.simpleframework</groupId>
<artifactId>simple-xml</artifactId>
<version>${simple-xml.version}</version>
</dependency>
Note: Latest version of Simple XML right now is 2.7.1.
Useful Resources
9. Conclusion
This was a quick intro to the XML ecosystem in Java. Use this as a guide to learn more about doing XML work and getting a high level view of the Java XML landscape.