How to convert XML file into properties file – Java

In last article, we show you how to convert properties file into XML file. See following XML file :


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
   <properties>
	<comment>Support Email</comment>
	<entry key="email.support">donot-spam-me@nospam.com</entry>
   </properties>

In this example, we show you how to use loadFromXML() method to load above XML file into a properties object, and get the key “email.support” value via getProperty() method.


package com.mkyong;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesXMLExample
{
    public static void main(String[] args) throws IOException
    {	
    	Properties props = new Properties();
    	
    	InputStream is = new FileInputStream("c:/email-configuration.xml");
    	//load the xml file into properties format
    	props.loadFromXML(is);
    	
    	String email = props.getProperty("email.support");
    	
    	System.out.println(email);
    	
    }
}

Output
The above example will print out the value of properties key : “email.support” :


donot-spam-me@nospam.com

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Kendly Paul
Guest
Kendly Paul

thank you!

Bh
Guest
Bh

is this works for multilevel?

Support Email
donot-spam-me@nospam.com

Anil
Guest
Anil

you forgot

 in.close() 
ShengLoong
Guest
ShengLoong

Hi Anil, I think we need not close the input stream manually.

As written in the JavaDoc, “The specified stream is closed after this method returns.”.

http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#loadFromXML(java.io.InputStream)