Java XML Tutorial with Examples

JAVA provides excellent support and a rich set of libraries to parse, modify or inquire XML documents. This tutorial will explain various types of Java-based parsers with examples in a simple and intuitive way. We will also learn how to convert an object to/from XML using JAXB API in Java.

In Java JDK, two built-in XML parsers are available – DOM and SAX, both have their pros and cons. Here are few examples to show how to create, modify and read an XML file with Java DOM, SAX, JDOM.

DOM XML Parser

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. DOM Parser is good for small files but when file size increases it performs slow and consumes more memory.
        This article shows you how to create an XML file in Java using DOM XML Parser
         This article explains how to use DOM Parser to create and write Object data to an XML file.
         Check out, DOM Parser can be used to modify existing XML data as well.

SAX Parser

SAX parser is work differently with DOM parser, it does not load any XML document into memory and create some object representation of the XML document. Instead, the SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure.
         Learn how to create our Callback Handler class to read XML file to list of Objects using SAX.

JDOM Parser

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.
         In this post, we will learn how to read XML file to Object using JDOM XML Parser.
         In this article, we will learn how to update the existing XML file in Java using JDOM.
         In this article, we will learn how to create an XML file in Java using JDOM Parser.

JAXB

Java Architecture for XML Binding (JAXB) is a Java standard that defines how Java objects are converted from and to XML. It uses a standard set of mappings. JAXB defines an API for reading and writing Java objects to and from XML documents.
          In this article, we will learn how to convert a Java object to XML and write to an external file.
          In this article, we will learn how to convert an XML to Java object using JAXB API.
        In this tutorial, we will learn how to convert Java objects from and to XML using JAXB API.

References

Learn all Java/J2EE Tutorial on All Java Tutorials

Comments