Java Convert Properties File to XML File

Java example to create XML file from Properties object or any existing .properties file.

Create XML File from Properties File

To convert properties file into XML file, best way is to use java.util.Properties class. Process is :

  1. Load properties file into java.util.java.util.Properties class object.
  2. Use Properties.storeToXML() method to write the content as XML.
package com.howtodoinjava.demo.xml;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import javax.xml.stream.XMLStreamException;

public class PropertiesToXML 
{
	public static void main(String[] args) throws XMLStreamException, 
                                     InvalidPropertiesFormatException, IOException 
	{
		String inPropertiesFile = "application.properties";
		String outXmlFile = "applicationProperties.xml";

		InputStream is = new FileInputStream(inPropertiesFile);	//Input file
		OutputStream os = new FileOutputStream(outXmlFile);		//Output file
		
		Properties props = new Properties();
		props.load(is);
		
		props.storeToXML(os, "application.properties","UTF-8");
	}
}

Input Properties File

#Disable batch job's auto start 
spring.batch.job.enabled=false
spring.main.banner-mode=off

#batch input files location
input.dir=c:/temp/input

Output XML File

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<comment>application.properties</comment>
	<entry key="input.dir">c:/temp/input</entry>
	<entry key="spring.batch.job.enabled">false</entry>
	<entry key="spring.main.banner-mode">off</entry>
</properties>

Drop me your questions in comment section.

Happy Learning !!

Leave a Reply

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial