In following the example, we define a JSON string that stores personal information: So the syntax of JSON is very simple. Each data information will have two parts: key and value which correspond to the field name and its value in a certain record. But as further looking, there are a few things like this:

The JSON string is enclosed by curly braces {}. The keys and values of JSON must be enclosed in quotation marks {“}. If there is more data (more key => value pairs), we use commas (,) to separate. JSON keys should be unsigned letters or numbers, _, and no spaces, the first character should not be set to numbers.

In this tutorial, you will learn-

What is JSON? What is XML?
What is Gson?
What is JAXB?
How to convert XML to JSON? Convert XML message to Java objects using JAXB Convert Java objects to JSON message using Gson Convert JSON message to Java objects using Gson Convert Java objects to XML message using JAXB

What is XML?

XML stands for eXtensible Markup Language also called the extensible markup language proposed by the World Wide Web Consortium (https://www.w3.org/) to create other markup languages. This is a simple subset that can describe many different types of data, so it is very useful in sharing data between systems. Tags in XML are often not predefined, but they are created according to user conventions. XML introduces new features based on the advantages of HTML. There are some more useful XML-making features in diverse systems and solutions:

XML is extensible: XML allows you to create your own custom tags to suit your application. XML carries data, not displaying it: XML allows you to store data regardless of how it will be displayed. XML is a common standard: XML was developed by the World Wide Web Consortium (W3C) and is available as an open standard.

XML is built on a nested node structure. Each node will have an opening tag and a closing tag as follows: In which:

is an open tag, the name of this tag is defined by you. is a closed tag, the name of this tag must match the name of the open tag. content is the content of this tag.

At the top of each XML file you must declare a tag to indicate the version XML is in use. The syntax of the instruction tag:

What is Gson?

Gson (https://github.com/google/gson) is a java library that allows users to convert from a Java object to JSON string and also convert from a JSON string to Java object. Gson can work with arbitrary Java objects including existing objects without you having their source-code. Since version 1.6, Gson introduces two new classes – JsonReader and JsonWriter to provide streaming processing on JSON data.

JsonWriter – Streaming write to JSON. The syntax for general implementation is as follows. We create a JsonWriter object. To start and finish creating a JSON string, we use the function beginObject() and endObject(). In the middle of executing these two functions, we perform writing data with pairs (key => value).

JsonReader – Streaming read from JSON. The syntax for general implementation is as follows. We create a JsonReader object. To start and finish creating a JSON string, we use the function beginObject() and endObject(). In the middle of executing these two functions, we perform reading data with pairs (key => value).

Gson streaming processing is fast. However you need to handle each pair (key => value) of processing JSON data.

What is JAXB?

JAXB stands for Java Architecture for XML Binding, which is a library that uses annotations to convert Java objects to XML content and vice versa. As JAXB is defined via a specification, we can use different implementations for this standard. With JAXB, we often use following basic annotations, namely:

@XmlRootElement: This annotation specifies what the outermost tag of the XML file is and therefore it is declared on top of a class. @XmlElementWrapper: This annotation creates a wrapper XML element around collections. @XmlElement: This annotation used to declare an attribute of the object is a tag of the XML file. @XmlAttribute: This annotation also used to declare an attribute of the object is a tag of the XML file.

The syntax for general implementation is as follows. First, we will initialize the JAXBContext object with the MyObject object to convert. In this JAXBContext object, it has a method to create an object that converts XML content to a Java object, Unmarshaller. In this JAXBContext object, it has a method to create the object that converts the Java object to the XML content that is Marshaller.

How to convert XML to JSON?

We implement the example of XML – JSON conversion on the platform:

Open JDK 8 for Ubuntu 18.04 x64. Eclipse IDE 2019-03 (4.11.0) x64 Java Development for Ubuntu. Gson 2.8.5.

Step 1. Create project. Create a new Java Project. Step 2. Set Project name. Set Project name is XmlToJsonExample. Step 3. Create a folder. Create folder data/input containing two file sample.xml and sample.json. Let’s first define our XML with department, role and person properties. The general architechture is: <one department – many roles> ; <one role – many persons>. Secondly, we define JSON having the same idea: Step 4. Define object. Define corresponding object classes in the package model.

Role.java:

Person.java:

Department.java:

XMLModel.java: Step 5. Set up library. Add and Set up library Gson 2.8.5 into Java Build Path.

Convert XML message to Java objects using JAXB

Firstly, we define performing classed in package service. At the first step of the first process, we use technique Un-marshalling of JAXB. Un-marshalling provides a client application the ability to convert XML data into JAXB derived Java objects. We define function getObjectFromXmlFile to un-marshal our XML file back to a Java object. This function is defined in class XMLService. We call the code above in class XmlToJsonService. Then we go to next step.

Convert Java objects to JSON message using Gson

At this step, we define function writeDataToJsonFile to write data to the JSON file. This function is defined in class JsonService. Note that to write a list of JSON strings, we use the function beginArray() and endArray(). Between these two functions, we write each JSON string. We call the above code in class XmlToJsonService. That’s the first process.

Convert JSON message to Java objects using Gson

At the first step of second process, we define function getDataFromJsonFile to read data from JSON file. This function is defined in class JsonService. Note that to read a list of JSON strings, we use the function beginArray() and endArray(). Between these two functions, we read each JSON string. We call the above code in class XmlToJsonService. Then we go to next step.

Convert Java objects to XML message using JAXB

At this step, we use technique Marshalling of JAXB. Marshalling provides a client application the ability to convert a JAXB derived Java object tree into XML data. We define function parseObjectToXm to marshall Java object to XML message. This function is defined in class We call the above code in class XmlToJsonService. That’s the second process.

Conclusion

In this tutorial, we briefly learned one way in which JAXB can read XML data and Gson write it to JSON. On the contrary, we also saw the way that Gson read JSON data and JAXB write it to XML.