A Guide to XML in Java

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.

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.

4. StAX Parser

A StAX Parser is median between DOM and SAX parser.

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>

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.

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.

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.

Leave a Reply

Your email address will not be published.