Properties
Convert XML File into Properties object
In the previous tutorial we saw how to convert Properties
object to XML Format and write it to XML File. In this tutorial we are going to show you how to the opposite, that is convert an XML File into a Properties
object in Java.
Here is the XML File that we’ve created in the previous tutorial :
emailProps.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>email</comment> <entry key="email">example@javacodegeeks.com</entry> </properties>
XMLFileToPropertiesExample.java
package com.javacodegeeks.java.core; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class XMLFileToPropertiesExample { private static final String xmlFilePath = "C:\\Users\\nikos7\\Desktop\\filesForExamples\\emailProps.xml"; public static void main(String[] args) throws IOException { Properties properties = new Properties(); FileInputStream fileStream = new FileInputStream(xmlFilePath); properties.loadFromXML(fileStream); String email = properties.getProperty("email"); System.out.println(email); } }
Output:
example@javacodegeeks.com
This was an example on how to