Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

Java Properties load from XML


How to load Properties in Java? Properties class has methods to load properties from different sources – InputStream, Reader, and XML file. Here’s an example of Properties.loadFromXML(InputStream).


In the post Java Properties load we’ve shown how to load properties from simple properties files. In this post we show how to load properties from XML file that can be created as in post Java Properties store as XML.

Sample XML properties with application configuration:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "">
<comment>Application properties as XML</comment>
<entry key="connectionTimeMillis">1000</entry>
<entry key="databaseURL">localhost:2097</entry>

Put the file in the project’s directory or src/main/resources if you’re using Maven or Gradle.

In the following code we use loadFromXML(InputStream) from java.util.Properties to load the file:


import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public class PropertiesLoadXML {

    public static void main(String[] args) throws IOException {
        Properties config = loadFromFile("app-config.xml");


    private static void printToSystemOutput(Properties config) throws IOException {, "Loaded properties:");

    private static Properties loadFromFile(String filename) throws IOException {
        Path configLocation = Paths.get(filename);
        System.out.println("Config location: " + configLocation.toString());
        try (InputStream stream = Files.newInputStream(configLocation)) {
            Properties config = new Properties();
            return config;

And here’s the output of running the above program:

Config location: app-config.xml
#Loaded properties:
#Sun Sep 13 13:33:12 CEST 2015
Share with the World!