In this article, we will discuss how to convert YAML file to XML file using ObjectMapper or Jackson library
1. Required libraries :
1.1 Maven Co-ordinates :
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>jackson-xml-databind</artifactId>
<version>0.6.2</version>
</dependency>
1.2 JAR files to download :
Download below jar files and include them in Project classpath –
- jackson-dataformat-yaml-2.13.4.jar
- jackson-annotations-2.13.4.jar
- jackson-core-2.13.4.jar
- jackson-databind-2.13.4.jar
- snakeyaml-1.13.jar
- jackson-core-asl-1.9.2.jar
- jackson-mapper-asl-1.9.2.jar
- jackson-xc-1.9.2.jar
- jackson-xml-databind-0.6.2.jar
- stax2-api-3.1.0.jar
- stax-api-1.0.2.jar
1.3 Project class-path :
2. YAML to XML conversion :
In the below illustration, we will convert YAML to XML file using Jackson or ObjectMapper,
- 1st step is to read the contents of the YAML file to a String object
- 2nd step is to convert YAML String into Person object using readValue() method of YamlMapper class by passing 2 input-arguments,
- YAML String
- Object type, in this case Person
- 3rd step is to convert Person object into XML using writeValueAsString() method of XmlMapper class by passing Person object as method–argument
- 4th step is to store converted XML into a file using Files.write() method passing 2 input-arguments
- File location
- XML String contents in bytes format
YAML file contents – YAML_Person.yaml:
---
title: "Mr."
firstName: "Sam"
lastName: "Anton"
address:
flatNumber: "BV-1025"
buildingName: "Shivaji"
plotNumber: "1093"
sector: "Sector 19"
city: "Bengaluru"
state: "Karnataka"
country: "India"
nodeName: "South Bengaluru"
items:
- itemName: "Television"
itemBoughtYear: "2014"
- itemName: "Washing Machine"
itemBoughtYear: "2020"
- itemName: "Refrigerator"
itemBoughtYear: "2011"
- itemName: "Grinder"
itemBoughtYear: "2012"
- itemName: "Computer"
itemBoughtYear: "2010"
YamlToXmlConversion.java
package in.bench.resources.yaml.conversion;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.fasterxml.jackson.xml.XmlMapper;
public class YamlToXmlConversion {
// main() method
public static void main(String[] args) {
try {
// 1st step - read YAML contents
String yamlContent = new String(Files.readAllBytes(Paths.get(
"C:\\Users\\Demo\\eclipse-workspace\\YamlConversion\\YAML_Person.yaml")));
// 2nd step - convert YAML to Person object using YAMLMapper
YAMLMapper yamlMapper = new YAMLMapper();
Person person = yamlMapper.readValue(yamlContent, Person.class);
// 3rd step - convert Person object into XML String using XmlMapper
XmlMapper xmlMapper = new XmlMapper();
String xml = xmlMapper
.writerWithDefaultPrettyPrinter() // pretty print
.writeValueAsString(person); // string
// print to console
System.out.println("YAML to XML conversion :- \n\n" + xml);
// store converted XML file
storeConvertedXmlInFile(xml);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method stores converted XML in Project class-path
*
* @param jsonString
*/
private static void storeConvertedXmlInFile(String xml) {
// define path to write Json file
Path filename = Paths.get(
"C:\\Users\\Demo\\eclipse-workspace\\YamlConversion\\YAML_to_XML_with_NS_Person.xml");
try {
// write to the above file path
Files.write(filename, xml.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output in console :
YAML to XML conversion :-
<Person xmlns="">
<title>Mr.</title>
<firstName>Sam</firstName>
<lastName>Anton</lastName>
<address>
<flatNumber>BV-1025</flatNumber>
<buildingName>Shivaji</buildingName>
<plotNumber>1093</plotNumber>
<sector>Sector 19</sector>
<city>Bengaluru</city>
<state>Karnataka</state>
<country>India</country>
<nodeName>South Bengaluru</nodeName>
</address>
<items>
<items>
<itemName>Television</itemName>
<itemBoughtYear>2014</itemBoughtYear>
</items>
<items>
<itemName>Washing Machine</itemName>
<itemBoughtYear>2020</itemBoughtYear>
</items>
<items>
<itemName>Refrigerator</itemName>
<itemBoughtYear>2011</itemBoughtYear>
</items>
<items>
<itemName>Grinder</itemName>
<itemBoughtYear>2012</itemBoughtYear>
</items>
<items>
<itemName>Computer</itemName>
<itemBoughtYear>2010</itemBoughtYear>
</items>
</items>
</Person>
Converted XML file -> YAML_to_XML_with_NS_Person.xml
<Person xmlns="">
<title>Mr.</title>
<firstName>Sam</firstName>
<lastName>Anton</lastName>
<address>
<flatNumber>BV-1025</flatNumber>
<buildingName>Shivaji</buildingName>
<plotNumber>1093</plotNumber>
<sector>Sector 19</sector>
<city>Bengaluru</city>
<state>Karnataka</state>
<country>India</country>
<nodeName>South Bengaluru</nodeName>
</address>
<items>
<items>
<itemName>Television</itemName>
<itemBoughtYear>2014</itemBoughtYear>
</items>
<items>
<itemName>Washing Machine</itemName>
<itemBoughtYear>2020</itemBoughtYear>
</items>
<items>
<itemName>Refrigerator</itemName>
<itemBoughtYear>2011</itemBoughtYear>
</items>
<items>
<itemName>Grinder</itemName>
<itemBoughtYear>2012</itemBoughtYear>
</items>
<items>
<itemName>Computer</itemName>
<itemBoughtYear>2010</itemBoughtYear>
</items>
</items>
</Person>
In the following article, we will discuss XML to YAML conversion using Jackson library in detail
Related Articles :
- Gson – Java Object to JSON conversion
- Jackson – Java Object to JSON conversion
- Java – How to convert an Object to YAML using ObjectMapper ?
- Java – How to convert YAML to an Object using ObjectMapper ?
- Java – How to convert YAML to JSON using ObjectMapper ?
- Java – How to convert JSON to YAML using ObjectMapper ?
- Java – How to convert XML to JSON using ObjectMapper ?
- Java – How to convert JSON to XML using ObjectMapper ?
- Java – How to convert XML to YAML using ObjectMapper ?
- Java – How to convert YAML to XML using ObjectMapper ?
- Java – How to pretty-print JSON using ObjectMapper ?
- Java – How to convert XML to JSON using JSONObject ?
- Java – How to convert JSON to XML using JSONObject ?
- Java – How to pretty-print JSON using JSONObject ?
References :
- https://www.json.org/json-en.html
- https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
- https://fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/jackson/databind/ObjectMapper.html
Happy Coding !!
Happy Learning !!