Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Properties store as XML

farenda 2015-09-10 0

Problem:

How to store Java Properties as XML? It turns out that it’s not only possible, but also easy with the Properties class as you can see in the example in this post.

Solution:

In post Java Properties store we’ve described how to store Properties as flat text files. In this post we’ll show how to store them as XML, which sometimes is very handy:

package com.farenda.java.util;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

public class PropertiesStoreXml {

    public static void main(String[] args) throws IOException {
        Properties config = createAppProperties();

        Path configFile = saveAsXml(config);

        displayFile(configFile);
    }

    private static Properties createAppProperties() {
        Properties config = new Properties();
        config.setProperty("connectionTimeMillis", "1000");
        config.setProperty("databaseURL", "localhost:2097");
        return config;
    }

    private static void displayFile(Path path) throws IOException {
        System.out.printf("%nContents of %s:%n", path.toString());
        Files.readAllLines(path).forEach(System.out::println);
    }

    private static Path saveAsXml(Properties config) throws IOException {
        Path file = Files.createTempFile("app-config", ".xml");
        try (OutputStream stream = Files.newOutputStream(file)) {
            config.storeToXML(stream, "Application properties as XML");
        }
        return file;
    }
}

Here we use Files and Path facilities available since Java 7. java.util.Properties class has two methods to store as XML:

  • void storeToXML(OutputStream os, String comment)
    Writes properties to OutputStream as XML with comment as comment element (see output) using UTF-8 encoding.
  • void storeToXML(OutputStream os, String comment, String encoding)
    Writes properties to OutputStream as XML with comment as comment element (see output), using provided encoding.

Running the example gives the following result:

Contents of /tmp/app-config7193299867429345100.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "https://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Application properties as XML</comment>
<entry key="connectionTimeMillis">1000</entry>
<entry key="databaseURL">localhost:2097</entry>
</properties>

Isn’t that cool? :-) Properties can be stored not only in flat text files, but also in XML format.

Share with the World!
Categories Java Tags java, java-basics
Previous: Java Properties store
Next: Java Properties load

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok