Properties to XML in java

This example shows you how to store Properties object’s key and value in XML file

Below is the example code:

package com.java.connect.io;

import java.io.FileOutputStream;
import java.util.Properties;

//Properties to XML in java
public class PropertiesToXmlExample {
	public static void main(String[] args) throws Exception {
		Properties properties = new Properties();
		properties.setProperty("person.name", "Mahendra SUDA");
		properties.setProperty("person.age", "29 years");
		properties.setProperty("person.occupation", "Software");

		FileOutputStream outputStream = new FileOutputStream(
				"PropertiesToXmlExample.xml");
		properties.storeToXML(outputStream, "Person xml file", "UTF-8");
	}
}

The output of the generated xml file looks like as:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Person xml file</comment>
<entry key="person.name">Mahendra SUDA</entry>
<entry key="person.age">29 years</entry>
<entry key="person.occupation">Software</entry>
</properties>