How To Convert Properties File Into XML File Using Java? | Java XML Tutorial



1. Introduction:  

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

2. Function Details:

Properties class has two variants of storeToXML function

public void storeToXML(OutputStream os,String comment) throws IOExceptionpublic void storeToXML(OutputStream os, String comment, String encoding) throws IOException

3. Source Code:

package com.example.TestApp;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Properties;public class PropertieFile2XMLFile { public static void main(String[] args) throws IOException {    Properties properties = new Properties();    properties.setProperty("username", "admin");    properties.setProperty("email", "admin@interviewbubble.com");    OutputStream os = new FileOutputStream("/Users/admin/Desktop/user.xml");    properties.storeToXML(os, "user detail", "UTF-8");    System.out.println("Saved user detail in XML file"); }}

4. OUTPUT:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties><comment>user detail</comment><entry key="email">admin@interviewbubble.com</entry><entry key="username">admin</entry></properties>