Java Convert XML to Properties – Read Properties from XML File

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

Properties to XML Example

To convert XML file into properties file, best way is to use java.util.Properties class. 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.
package com.howtodoinjava.demo;

import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class XMLToProperties 
	public static void main(String[] args) throws InvalidPropertiesFormatException, IOException 
		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'

Input XML 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>

Output Properties File

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


Drop me your questions in comment section.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

1 thought on “Java Convert XML to Properties – Read Properties from XML File”

Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.