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

Convert XML to Properties


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

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

public class XMLToProperty {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties properties = null;
InputStream inputStream = null;

try {
properties = new Properties();

inputStream = new FileInputStream("C:\\jtc\\log4j.xml");
properties.loadFromXML(inputStream);
String rootLogger = properties.getProperty("log4j.rootLogger");
System.out.println("Root logger : " + rootLogger);
String appender = properties.getProperty("log4j.appender.HTML");
System.out.println("Appender : " + appender);

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

}

}

In above program loadFromXML() methods loads xml content into property instance getProperty() property method used to get property using key.The log4j.xml file contains following content

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>

Now we can run the program see the following output in console.

Root logger : DEBUG, HTML

Appender : org.apache.log4j.FileAppender

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to find parent directory of file using JavaString java.io.File.getParent()Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory. The parent of an abstract pathname consists of the pathname's prefix, i… Read More
  • How to delete directory using Java boolean java.io.File.delete() Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Note that the java.nio.file.Fil… Read More
  • Java Directory Examples How to Create Directory How to retrieve all the files from Directory How to search file from directory How to retrieve all the files from Directory using File.listFiles() How to retrieve all the directory names How to retr… Read More
  • How to find the size of directory using FileUtils public static long sizeOfDirectory(File directory) Counts the size of a directory recursively (sum of the length of all files). Note that overflow is not detected, and the return value may be negative if overflow occurs. See… Read More
  • How to check directory or file is hidden using Java boolean java.io.File.isHidden() Tests whether the file named by this abstract pathname is a hidden file. The exact definition of hidden is system-dependent. On UNIX systems, a file is considered to be hidden if its name beg… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Convert XML to Properties Rating: 5 Reviewed By: eHowToNow