Java – How to convert XML to YAML using Jackson ?

In this article, we will discuss how to convert XML file to YAML 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

1.3 Project class-path :

2. XML to YAML conversion :

In the below illustration, we will convert XML to YAML file using Jackson or XmlMapper/YamlMapper,

  • 1st step is to read the contents of the XML file to a String object
  • 2nd step is to convert XML String into Person object using readValue() method of XmlMapper class by passing 2 input-arguments,
    1. XML String
    2. Object type, in this case Person
  • 3rd step is to convert Person object into YAML using writeValueAsString() method of YamlMapper class by passing Person object as methodargument
  • 4th step is to store converted YAML into a file using Files.write() method passing 2 input-arguments
    • File location
    • YAML String contents in bytes format

XML file contents – Person_with_NS.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>

XmlToYamlConversion.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 XmlToYamlConversion {

	// main() method
	public static void main(String[] args) {

		try {
			// 1st step - read XML contents
			String xmlContent = new String(Files.readAllBytes(Paths.get(
					"C:\\Users\\Demo\\eclipse-workspace\\YamlConversion\\Person_with_NS.xml")));


			// 2nd step - convert XML to Person object
			XmlMapper xmlMapper = new XmlMapper();
			Person person = xmlMapper.readValue(xmlContent, Person.class);


			// 3rd step - convert Person object into YAML String
			YAMLMapper yamlMapper = new YAMLMapper();
			String yaml = yamlMapper
					.writerWithDefaultPrettyPrinter() // pretty print
					.writeValueAsString(person); // string


			// print to console
			System.out.println("XML to YAML conversion :- \n\n" + yaml);


			// store converted YAML file
			storeConvertedYamlInFile(yaml);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	/**
	 * This method stores converted YAML in Project class-path
	 * 
	 * @param jsonString
	 */
	private static void storeConvertedYamlInFile(String yaml) {

		// define path to write Json file
		Path filename = Paths.get(
				"C:\\Users\\Demo\\eclipse-workspace\\YamlConversion\\XML_with_NS_to_YAML_Person.yaml");

		try {

			// write to the above file path
			Files.write(filename, yaml.getBytes());

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output in console :

XML to YAML conversion :- 

---
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"

Converted YAML file -> XML_with_NS_to_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"

In the following article, we will discuss YAML to XML conversion using Jackson library in detail

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java – How to convert YAML to XML using Jackson ?
Java – How to pretty-print JSON using ObjectMapper ?