Home » Core Java » util » Properties » Convert XML File into Properties object

About Nikos Maravitsas

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.

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close