How to convert properties file into XML file – Java

Many developers may not aware of this function, actually, the java.util.Properties class come with a storeToXML() method to convert existing properties data into a XML file.

Note
Please refer to this Properties JavaDoc for detail explanation.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class PropertiesXMLExample
{
    public static void main(String[] args) throws IOException
    {	
    	Properties props = new Properties();
    	props.setProperty("email.support", "donot-spam-me@nospam.com");
    	
    	//where to store?
    	OutputStream os = new FileOutputStream("c:/email-configuration.xml");
    	
    	//store the properties detail into a pre-defined XML file
    	props.storeToXML(os, "Support Email","UTF-8");
    	
    	System.out.println("Done");
    }
}

The above example will write the properties detail into a XML file “c:/email-configuration.xml“.


<?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>

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
Agila
Guest
Agila

hi ..may i know is it possible to use property file data in XML ?

Saurabh
Guest
Saurabh

awesome tutorial…new thing came to know

James
Guest
James

To View and validate XML Data, you can use this tool. http://codebeautify.org/xmlviewer

Michael
Guest
Michael

yong

hello world ’ foo & bar

honololo

100000

how i can parse this xml file through sax parser. n how to handle reference characters i.e ’

trackback
Java XML Tutorial | Dev Solver

[…] Convert properties file into XML file […]

david
Guest
david

A person essentially help to make severely articles I’d state. This is the first time I frequented your web page and to this point? I surprised with the analysis you made to make this actual put up amazing. Magnificent job!

Haritha
Guest
Haritha

If my xml is of below,How can i write the above java code????

 ..how can write


  
    
  

 
majidyaghoubi
Guest
majidyaghoubi

hi,
Thank you for your very helpful site.

trackback
How to load properties from XML file?

[…] last article, it shows how to store properties file into XML file, and generate the following XML file : <?xml version="1.0" […]