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.




Java Code


To convert the properties file to xml file is really ease, just call: storeToXML()


package com.camelcode;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
 
public class PropertiesToXml {
 
    public static void main(String[] args) throws IOException {
 
        Properties prop = new Properties();
 
        prop.setProperty("one-two", "buckle my shoe");
        prop.setProperty("three-four", "shut the door");
        prop.setProperty("five-six", "pick up sticks");
        prop.setProperty("seven-eight", "lay them straight");
        prop.setProperty("nine-ten", "a big, fat hen");
 
        OutputStream os = new FileOutputStream("Rhyme.xml");
        prop.storeToXML(os, "Rhyme","UTF-8");
 
        System.out.println("Rhyme created");
    }
}


Generated file


The file that's been generated




Download


Download source code - Convert-propertiesfile-to-xml-file.zip  2 KB