Jackson is one of the popular JSON Parser for Java application, with the release of Jackson 2 they have added the support of XML Parsing. In this article, we will learn how to convert XML to JSON using Jackson 2 library.
Folder Structure:
- Create a simple Maven Project “Jackson2XmlToJson” by selecting maven-archetype-quickstart and create a package for our source files “com.javainterviewpoint” under src/main/java
- Now add the following dependency in the POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javainterviewpoint</groupId> <artifactId>Jackson2XmlToJson</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Jackson2XmlToJson</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> </dependencies> </project>
- Create the Java classes XmlToJson.java and XmlFileToJson.java under com.javainterviewpoint folder.
Convert XML to JSON using Jackson 2
To parse XML to JSON, We will be using the below XML
XML file content (student.xml)
<?xml version='1.0' encoding='UTF-8'?> <student> <age>33</age> <id>44</id> <name>JavaInterviewPoint</name> </student>
XmlToJson.java
package com.javainterviewpoint; import java.io.IOException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper; public class XmlToJson { public static void main(String[] args) { String data = "<?xml version='1.0' encoding='UTF-8'?>"+ "<student>"+ "<age>11</age>"+ "<id>12</id>"+ "<name>JavaInterviewPoint</name>"+ "</student>"; try { // Create a new XmlMapper to read XML tags XmlMapper xmlMapper = new XmlMapper(); //Reading the XML JsonNode jsonNode = xmlMapper.readTree(data.getBytes()); //Create a new ObjectMapper ObjectMapper objectMapper = new ObjectMapper(); //Get JSON as a string String value = objectMapper.writeValueAsString(jsonNode); System.out.println("*** Converting XML to JSON ***"); System.out.println(value); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
In the above code, we will be converting the simple XML string to JSON format
- In Jackson 2 we have an XmlMapper class to work with XML, just create an instance of the XmlMapper class and call the readTree() method on top of it to get the JsonNode.
XmlMapper xmlMapper = new XmlMapper(); JsonNode jsonNode = xmlMapper.readTree(data.getBytes());
- Create a new ObjectMapper object, it helps us mapping the JSON data
ObjectMapper objectMapper = new ObjectMapper();
- The JsonNode can be converted into a string using the writeValueAsString() of the ObjectMapper
String value = objectMapper.writeValueAsString(jsonNode); System.out.println(value);
Output:
*** Converting XML to JSON *** {"age":"11","id":"12","name":"JavaInterviewPoint"}
Convert XML file to JSON in Java
package com.javainterviewpoint; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper; public class XmlFileToJson { public static void main(String[] args) { String data = ""; try { // Read the student.xml data = FileUtils.readFileToString(new File("c:\\student.xml"), "UTF-8"); // Create a new XmlMapper to read XML tags XmlMapper xmlMapper = new XmlMapper(); //Reading the XML JsonNode jsonNode = xmlMapper.readTree(data.getBytes()); //Create a new ObjectMapper ObjectMapper objectMapper = new ObjectMapper(); String value = objectMapper.writeValueAsString(jsonNode); System.out.println("*** Converting XML to JSON ***"); System.out.println(value); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
- In order to read the XML file, we will be using FileUtils.readFileToString () method [commons.io jar]
data = FileUtils.readFileToString(new File("c:\\Jackson\\student.xml"), "UTF-8");
- Create an instance of the XmlMapper class and call the readTree() method on top of it to get the JsonNode.
XmlMapper xmlMapper = new XmlMapper(); JsonNode jsonNode = xmlMapper.readTree(data.getBytes());
- Create a new ObjectMapper object, it helps us mapping the JSON data
ObjectMapper objectMapper = new ObjectMapper();
- The JsonNode can be converted into a string using the writeValueAsString() of the ObjectMapper
String value = objectMapper.writeValueAsString(jsonNode); System.out.println(value);
Output:
*** Converting XML to JSON *** {"age":"33","id":"44","name":"JavaInterviewPoint"}
Leave a Reply