Java - How to convert XML to JSON with Jackson

In this section, we will show you how to convert XML to JSON using Jackson.

Note


Extensible Markup Language (XML) lets you define and store data in a shareable manner. XML supports information exchange between computer systems such as websites, databases, and third-party applications. Predefined rules make it easy to transmit data as XML files over any network because the recipient can use those rules to read the data accurately and efficiently.

Note


JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging.

JSON is built on two structures: 

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. 
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Download Jackson

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.14.1</version>
</dependency>

User.java

public class User {

private String name;
private String email;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}


Main.java

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class Main {
public static void main(String[] args)
throws JsonProcessingException {

String xml = "<User>\n" +
" <name>Sibin</name>\n" +
" <email>sibin@gmail.in</email>\n" +
" </User>";

//Parse the XML into a User instance
XmlMapper xmlMapper = new XmlMapper();
User user = xmlMapper.readValue(xml, User.class);

/* Once we have our User instance,
we'll want to write it out as JSON
using the familiar ObjectMapper:
*/
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
System.out.println(json);
}
}

Console Output:
{"name":"Sibin","email":"sibin@gmail.in"}

More related topics,

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

ReactJS - Bootstrap - Buttons

Spring Core | BeanFactoryPostProcessor | Example

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Custom Exception Handling in Quarkus REST API

ReactJS, Spring Boot JWT Authentication Example

Java, Spring Boot Mini Project - Library Management System - Download

Top 5 Java ORM tools - 2024

Java - DES Encryption and Decryption example