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 a few examples to show how to create, modify and read an XML file with Java DOM, SAX, and 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 slowly and consumes more memory.
        This article shows you how to create an XML file in Java using DOM XML Parser
         Check out, DOM Parser can be used to modify existing XML data as well.
         This article explains how to use DOM Parser to create and write Object data to an XML file.

SAX Parser

SAX parser works differently from the DOM parser, it does not load any XML document into memory and creates some object representation of the XML document. Instead, the SAX parser uses the callback function (org.xml.sax.helpers.DefaultHandler) to inform clients of the XML document structure.
         Learn how to create our Callback Handler class to read XML files to a 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 choose 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 files to Objects 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 a Java object using JAXB API.
        In this tutorial, we will learn how to convert Java objects from and to XML using JAXB API.

Latest JAXB Examples using Java 11

  • JAXB - How to Convert Java Object to/from XML - In this tutorial, we will show you how to use the JAXB API to convert Java Object to/from XML. In this tutorial example, we use JDK 11 and Maven to create our applications.
  • Java JAXB Example Tutorial (Java 11) - In this tutorial, we will show you how to use the JAXB API to convert Java Object to/from XML. In this tutorial example, we use JDK 11 and Maven to create our applications.

References

Learn all Java/J2EE Tutorial on All Java Tutorials

Comments