Java Convert XML to Properties – Read Properties from XML File

Java example to create .properties file from a given XML file. This code can be used to read properties key-values from XML file, to be used in the application code.

To convert a properties file to XML file, follow the steps given in the linked tutorial.

1. Convert from Properties to XML Example

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

The process is :

  1. Load XML file into java.util.Properties class object, using Properties.loadFromXML() method.
  2. Use method to write the content as properties.
String outPropertiesFile = "";
String inXmlFile = "applicationProperties.xml";

InputStream inStream = new FileInputStream(inXmlFile);      //Input XML File
OutputStream outStream = new FileOutputStream(outPropertiesFile); //Output properties File

Properties props = new Properties();

//Load XML file

//Store to properties file, "Converted from applicationProperties.xml");

//Use properties in code
System.out.println(props.get("input.dir"));     //Prints 'c:/temp/input'

2. Demo

The input XML file is as follows that we will convert to the properties file.

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

The converted output properties file is as follows:

#Converted from applicationProperties.xml
#Mon Jul 23 18:15:00 IST 2018

Drop me your questions in the comment section.

Happy Learning !!


Notify of
1 Comment
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.