How To Convert Properties File Into XML File Using Java?

In my previous tutorials I have explained about how to use DOM Parser and SAX Parser. This tutorial explain with very simple utility method available in the Java package used for converting your existing properties file to a XML file. Sometimes this may be the requirement for Java developers, instead of manually converting properties file to XML, you can use this simple utility class to input the properties file and get the result in the XML file. This utility available in the java.util.Properties and the method name is storeToXML(). Look at the below example.

1. Create A Simple Properties File


2. Write Utility For Conversion


import java.util.Properties;

public class PropToXmlDemo {
	public static void main(String[] args) throws IOException
    	Properties props = new Properties();
    	}catch (Exception e){
    	//Path for the output file
    	File file = new File("msg.xml");
    	OutputStream out = new FileOutputStream(file);
     	//Call the utility method to store the properties values to XML file
    	props.storeToXML(out, "Message Properties","UTF-8");


3. Outout XML File

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "">
    <comment>Message Properties</comment>
    <entry key="key3">Message3</entry>
    <entry key="key2">Message2</entry>
    <entry key="key1">Message1</entry>

4. Folder Structure

Look at the folder structure for the above example.

Properties To XML File Folder Structure

If you have any questions, please write it in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!