We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 26 October 2013

Convert Property File to XML


In this tutorial we are going to see how to convert Property file into XML using Java
1. Create project called JavaXML.
2. Create package called com.javatutorialscorner.xml
3. Create java class called PropertyToXML under com.javatutorialscorner.xml
PropertyToXML .java
package com.javatutorialscorner.xml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class PropertyToXML {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Properties properties = null;
OutputStream outputStream = null;

try {
properties = new Properties();
properties.load(new FileReader("C:\\jtc\\log4j.properties"));
outputStream = new FileOutputStream("C:\\jtc\\log4j.xml");
properties.storeToXML(outputStream, "Log4j XML", "UTF-8");

System.out.println("XML File Created");

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

In above program we are going to convert log4j.properties file to log4j.xml.(Sample property and XML files).The log4j.properties contains following content.The storeToXML() method used to write xml file from property file.

log4j.properties

logFile = C:/log
log4j.rootLogger = DEBUG, HTML

log4j.appender.HTML=org.apache.log4j.FileAppender
log4j.appender.HTML.File=${logFile}/javatutorialscorner.html

log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Java Tutorials Corner - Log4j HTML Layout Example

4. Now we can run the program as the result log4j.xml file created at specified location.log4j.xml file contains following content.(This is not real log4j configuration, just converted xml file)

log4j.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Log4j XML</comment>
<entry key="log4j.rootLogger">DEBUG, HTML</entry>
<entry key="log4j.appender.HTML">org.apache.log4j.FileAppender</entry>
<entry key="logFile">C:/log</entry>
<entry key="log4j.appender.HTML.File">${logFile}/javatutorialscorner.html</entry>
<entry key="log4j.appender.HTML.layout">org.apache.log4j.HTMLLayout</entry>
<entry key="log4j.appender.HTML.layout.Title">Java Tutorials Corner - Log4j HTML Layout Example</entry>
</properties>

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Convert Property File to XML Rating: 5 Reviewed By: eHowToNow