Java Convert Properties File to XML File

Java example to create XML file from Properties object or any existing .properties file. To convert an XML file to .properties file, follow the steps given in linked tutorial.

1. Create XML File from Properties File

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

The 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.
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);

//properties to XML conversion
props.storeToXML(os, "application.properties","UTF-8");

2. Demo

The Input Properties File is as follows which we will convert to XML.

#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

The output XML file after the conversion is as follows:

<?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 the comment section.

Happy Learning !!

Comments

Subscribe
Notify of
guest
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

Dark Mode

Dark Mode