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

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button