Properties file in XML in Java

Properties can be stored in XML file. In this article, properties file in XML format is explained.



How to store properties in XML format

The storeToXML method in the Property class allows configuration to be stored  in xml format. The xml format will be shown as below.

PropertyFile/src/com/yawintutor/CreateXMLFileFromProperty.java

package com.yawintutor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class CreateXMLFileFromProperty {
	public static void main(String[] args) {
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(new File("configuration2.properties"));
			Properties properties = new Properties();
			properties.put("buildversion", "1.0.0");
			properties.put("installversion", "2.0.0");

			properties.storeToXML(fos, "test file");
			System.out.println("Properties file is created successfully");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

Output

Properties file is created successfully

PropertyFile/configuration2.properties

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>test file</comment>
<entry key="buildversion">1.0.0</entry>
<entry key="installversion">2.0.0</entry>
</properties>


How to read properties from xml file

The xml format below can be used as configuration properties. Properties class will load this xml file’s configuration.

PropertyFile/src/com/yawintutor/PropertyFileReadFromXMLFile.java

package com.yawintutor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class PropertyFileReadFromXMLFile {
	public static void main(String[] args) {
		load("configuration1.properties");
	}

	public static void load(String fileName) {
		FileInputStream fis;
		try {
			fis = new FileInputStream(new File(fileName));
			Properties properties = new Properties();
			properties.loadFromXML(fis);

			Enumeration<?> enumKey = properties.propertyNames();
			while (enumKey.hasMoreElements()) {
				String key = (String) enumKey.nextElement();
				String value = properties.getProperty(key);
				System.out.println("Key : " + key + ", Value : " + value);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

PropertyFile/configuration1.properties

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>test file</comment>
<entry key="buildversion">1.0.0</entry>
<entry key="installversion">2.0.0</entry>
</properties>

Output

Key : buildversion, Value : 1.0.0
Key : installversion, Value : 2.0.0



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *