HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java XML / Java Convert Properties File to XML File

Java Convert Properties File to XML File

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

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

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

Feedback, Discussion and Comments

  1. Davvy

    September 3, 2019

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

  2. krishma sood

    May 24, 2019

    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

Comments are closed on this article!

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?

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

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 © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces