• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To Convert Properties File Into XML File Using Java?

October 27, 2013 //  by Krishna Srinivasan//  Leave a Comment

In my previous tutorials I have explained about how to use DOM Parser and SAX Parser. This tutorial explain with very simple utility method available in the Java package used for converting your existing properties file to a XML file. Sometimes this may be the requirement for Java developers, instead of manually converting properties file to XML, you can use this simple utility class to input the properties file and get the result in the XML file. This utility available in the java.util.Properties and the method name is storeToXML(). Look at the below example.

1. Create A Simple Properties File

key1=Message1
key2=Message2
key3=Message3

2. Write Utility For Conversion

PropToXmlDemo.java

package javabeat.net.xml;

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

public class PropToXmlDemo {
	public static void main(String[] args) throws IOException
    {
    	Properties props = new Properties();
    	try{
    	props.load(PropToXmlDemo.class.getClassLoader().getResourceAsStream("javabeat/net/xml/messages.properties"));
    	}catch (Exception e){
    		e.printStackTrace();
    	}
    	//Path for the output file
    	File file = new File("msg.xml");
    	file.createNewFile();
    	OutputStream out = new FileOutputStream(file);
     	//Call the utility method to store the properties values to XML file
    	props.storeToXML(out, "Message Properties","UTF-8");
    }

}

3. Outout XML File

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>Message Properties</comment>
    <entry key="key3">Message3</entry>
    <entry key="key2">Message2</entry>
    <entry key="key1">Message1</entry>
</properties>

4. Folder Structure

Look at the folder structure for the above example.

Properties To XML File Folder Structure

If you have any questions, please write it in the comments section.

Category: XMLTag: XML

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How to Parse XML file using SAX Parser?
Next Post: How to Create PDF using iText in Java? »

Reader Interactions

Leave a Reply Cancel reply

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

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

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact