How to convert properties file into XML file in Java

  • 04 April 2017
  • ADM

 

How to convert properties file into XML file in Java - images/logos/java.jpg

 

The java.util.Properties class come with a storeToXML() method to convert existing properties data into an XML file.

Example

package com.admfactory.basic;

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

public class PropertiesToXML {

    public static void main(String[] args) throws Exception {
	System.out.println("Properties to XML Example");
	System.out.println();
	Properties props = new Properties();
	props.setProperty("username", "admin");
	props.setProperty("email", "admin@admfactory.com");

	OutputStream os = new FileOutputStream("d:/admfactory.com/config.xml");

	/** store the properties detail into a pre-defined XML file */
	props.storeToXML(os, "User list", "UTF-8");

	System.out.println("Done!");
    }

}

Output

Here is the content of the file created.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>User list</comment>
<entry key="email">admin@admfactory.com</entry>
<entry key="username">admin</entry>
</properties>

 

References