HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java Convert Properties File to XML File

By Lokesh Gupta | Filed Under: XML

Java example to create XML file from Properties object or any existing .properties file.

Create XML File from Properties File

To convert properties file into XML file, best way is to use java.util.Properties class. Process is :

  1. Load properties file into java.util.java.util.Properties class object.
  2. Use Properties.storeToXML() method to write the content as XML.
package com.howtodoinjava.demo.xml;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import javax.xml.stream.XMLStreamException;

public class PropertiesToXML 
{
	public static void main(String[] args) throws XMLStreamException, 
                                     InvalidPropertiesFormatException, IOException 
	{
		String inPropertiesFile = "application.properties";
		String outXmlFile = "applicationProperties.xml";

		InputStream is = new FileInputStream(inPropertiesFile);	//Input file
		OutputStream os = new FileOutputStream(outXmlFile);		//Output file
		
		Properties props = new Properties();
		props.load(is);
		
		props.storeToXML(os, "application.properties","UTF-8");
	}
}

Input Properties File

#Disable batch job's auto start 
spring.batch.job.enabled=false
spring.main.banner-mode=off

#batch input files location
input.dir=c:/temp/input

Output XML File

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<comment>application.properties</comment>
	<entry key="input.dir">c:/temp/input</entry>
	<entry key="spring.batch.job.enabled">false</entry>
	<entry key="spring.main.banner-mode">off</entry>
</properties>

Drop me your questions in comment section.

Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

2
Leave a Reply

This comment form is under antispam protection
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Davvy

I would recommend “Total XML converter”. It works pretty cool for me. They also offer free trial version. First try then buy.

Vote Up0Vote Down  Reply
3 months ago
krishma sood

Hello,

Can you help me generating a XML from XPATHS and value collection or properties.

e.g. a simple use case – it can be a collection of key value like below

/root/abc/def/ghi=value1
/root/abc/def/ggg=value2
/root/abc/kkk/ggg=value3
/root/abc/kkk/hhh/iii=value4
/root/addr/kkk/ggg=value3
…..

only root tag is common and inside it can be any hierarchy.

Java code so appropriate hierarchical xml can be generated

Thanks

Vote Up0Vote Down  Reply
6 months ago

Search Tutorials

Java XML Tutorial

  • Java – Read XML DOM Parser
  • Java – Read XML SAX Parser
  • Java – Read XML JDOM2 Parser
  • Java – Read XML StAX Parser
  • Java – DOM vs SAX Parser
  • Java – Convert XML to Properties
  • Java – Convert Properties to XML
  • Java – Convert String to XML
  • Java – Convert XML to String
  • Java – XPath Tutorial
  • Java – Evaluate XPath on DOM
  • Java – Evaluate XPath on String
  • Java – XPath Examples
  • Java – XPath NamespaceContext
  • Java – Get Attribute using XPath
  • Java – XPath Attribute Examples
  • Java – Check if XML tag exists?

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz