Java example to create XML file from Properties
object or any existing .properties
file. To convert an XML file to .properties file, follow the steps given in linked tutorial.
1. Create XML File from Properties File
To convert the properties file into an XML file, the best way is to use java.util.Properties
class.
The process is :
- Load properties file into
java.util.
object.java.util.Properties
class - Use
Properties.storeToXML()
method to write the content as XML.
String inPropertiesFile = "application.properties";
String outXmlFile = "applicationProperties.xml";
InputStream is = new FileInputStream(inPropertiesFile); //Input file
OutputStream os = new FileOutputStream(outXmlFile); //Output file
Properties props = new Properties();
props.load(is);
//properties to XML conversion
props.storeToXML(os, "application.properties","UTF-8");
2. Demo
The Input Properties File is as follows which we will convert to XML.
#Disable batch job's auto start
spring.batch.job.enabled=false
spring.main.banner-mode=off
#batch input files location
input.dir=c:/temp/input
The output XML file after the conversion is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>application.properties</comment>
<entry key="input.dir">c:/temp/input</entry>
<entry key="spring.batch.job.enabled">false</entry>
<entry key="spring.main.banner-mode">off</entry>
</properties>
Drop me your questions in the comment section.
Happy Learning !!