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;import;import;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", "");    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"); }}


<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE properties SYSTEM ""><properties><comment>user detail</comment><entry key="email"></entry><entry key="username">admin</entry></properties>