Showing posts with label Java Properties. Show all posts
Showing posts with label Java Properties. Show all posts

How to convert Java Properties file to XML file

 
Convert Java Properties file to XML file

In our earlier tutorial we have seen how to convert XML file to properties file. As a vice verse we will see how to convert XML file to java properties file in this tutorial. Lets consider there are property key with designation, city, state and country as same as previous tutorial example. Lets try to create same XML file using java properties file in below example code,


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

public class PropertyToXML {
 
 public static void main(String[] args) {
  try{
   Properties property = new Properties();
   property.setProperty("designation", "APAC Manager");
   property.setProperty("city", "Bangalore");
   property.setProperty("state", "Karnataka");
   property.setProperty("country", "India");
  
   // XML file path to save 
   OutputStream os = new FileOutputStream("D://propertiesToXML.xml");
    
   // Write properties into XML
      property.storeToXML (os, "Details");
      
  }catch (Exception e) {
   e.printStackTrace();
  }
 }
}


OUTPUT:





How to convert XML file to Java Properties file

 
Convert XML file to Java Properties file
In one of our earlier tutorial we have seen how to use Properties class in Java to read property file. Basically property file hold key and corresponding  value part and by just loading those property files we can read application or project related properties which we have configured and also easy to modify those values. 
Same way by using Properties class we can convert XML file to Properties file and also vice-verse (Properties file to XML file). In this tutorial we will see about how to convert XML file to Java Properties file.

Input XML File:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>
 <entry key="designation">Software Engineer</entry>
 <entry key="city">Bangalore</entry>
 <entry key="state">Karnataka</entry>
 <entry key="country">India</entry>
</properties>


Java Code to convert XML file to Java Properties file:


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class XMLToProperties {
 
 public static void main(String[] args) {
  try{
   Properties property = new Properties();
   InputStream is = new FileInputStream(new File("D://myproperties.xml"));
   property.loadFromXML(is);
   
   String designation = property.getProperty("designation");
   String city = property.getProperty("city");
   String state = property.getProperty("state");
   String country = property.getProperty("country");
   
   System.out.println(designation);
   System.out.println(city);
   System.out.println(state);
   System.out.println(country);
   
  }catch (Exception e) {
   e.printStackTrace();
  }
 } 
}


OUTPUT:


Software Engineer
Bangalore
Karnataka
India


In XML file <properties> and <entry> tag name must be same along with "key" attribute in entry tag which will be identified as key and value properties.  




Using Java Properties class


In this tutorial we will see about Java Properties class and how to use in our project to read property file values. Most of the time in our project we will be use configuration parameters like database connection details or other parameter related to project. Set the parameters in property file and read throughout the application using Java Properties.

Where here values can be easily modified or even we can add new parameters to the project through property file at run-time without reloading the context or editing any java file. 

project.properties


driver=org.postgresql.Driver
url=jdbc:postgresql://localhost/databasseName
user=postgres
password=postgres
maxconn=10
maxidle=0


import java.io.InputStream;
import java.util.Properties;

public class PropertiesTest {

 public static void main(String[] args) {
  
  String propertyFile = "project.properties";
  
  Properties property = new PropertiesTest().readPropertyFile(propertyFile);
  
  System.out.println("DRIVER   : "+property.getProperty("driver"));
  System.out.println("URL      : "+property.getProperty("url"));
  System.out.println("USER     : "+property.getProperty("user"));
  System.out.println("PASSWORD : "+property.getProperty("password"));
  System.out.println("MAX CONN : "+property.getProperty("maxconn"));
  System.out.println("MAX IDLE : "+property.getProperty("maxidle"));
 }
 
 public Properties readPropertyFile(String propertyFile){
  Properties properties = new Properties();
  InputStream inputstream = getClass().getResourceAsStream(propertyFile);
  try {
   properties.load(inputstream);
   System.out.println("NO OF PROPERTIES ::::: " + properties.size());   
  } catch (Exception e) {
   e.printStackTrace();
  }
  return properties;
 }
}


OUTPUT:


NO OF PROPERTIES ::::: 6
DRIVER   : org.postgresql.Driver
URL      : jdbc:postgresql://localhost/databasseName
USER     : postgres
PASSWORD : postgres
MAX CONN : 10
MAX IDLE : 0