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

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

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 ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

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
Inline Feedbacks
View all comments
Back to top button