Java – Convert properties file to XML

In Java, we can use the Properties#storeToXML() to convert properties values into an XML file.

Table of contents

P.S Tested with Java 11.

1. Convert properties values to XML file

The below example creates some property values and stores them as an XML file.

PropertiesToXml.java

package com.mkyong.xml.tips;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class PropertiesToXml {

  public static void main(String[] args) throws IOException {

      // create some properties values on demand
      Properties props = new Properties();
      props.setProperty("email.support", "[email protected]");
      props.setProperty("http.port", "8080");
      props.setProperty("http.server", "localhost");

      try (OutputStream output =
          new FileOutputStream("c:\\test\\server-config.xml")) {

          // convert the properties to an XML file
          props.storeToXML(output, "Server config file", StandardCharsets.UTF_8);

      }

  }

}

Output

c:\\test\\server-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Server config file</comment>
<entry key="http.port">8080</entry>
<entry key="email.support">[email protected]</entry>
<entry key="http.server">localhost</entry>
</properties>

2. Convert a properties file to an XML file

The below example loads a .properties file and stores them as an XML file.

application.properties

greeting.message=hello
quarkus.http.port=8080
PropertiesToXml2.java

package com.mkyong.xml.tips;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class PropertiesToXml2 {

  public static void main(String[] args) throws IOException {

      Properties props = new Properties();
      try (InputStream input =
            new FileInputStream("src/main/resources/application.properties")) {
          // loads a properties file
          props.load(input);
      }

      try (OutputStream output =
            new FileOutputStream("c:\\test\\server-config.xml")) {

          // convert the properties to an XML file
          props.storeToXML(output, "Server config file",
                  StandardCharsets.UTF_8);

      }

  }

}

Output

c:\\test\\server-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Server config file</comment>
<entry key="greeting.message">hello</entry>
<entry key="quarkus.http.port">8080</entry>
</properties>

3. Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-xml

$ cd src/main/java/com/mkyong/xml/tips/

4. References

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments