Java – Load Properties from a XML file

Properties from XML file can be read using loadFromXML() option

 
package com.pwn.property;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class LoadPropertyFromXML {

	public static void main(String[] args) {
		// make new properties instance to load the file into
		Properties prop = new Properties();
		// check to make sure the file exists
		File file = new File("c://install//userdetails.xml");
		if (file.exists()) {
			// load the file
			try {
				prop.loadFromXML(new FileInputStream(file));
			} catch (InvalidPropertiesFormatException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			// print out all the properties
			for (String name : prop.stringPropertyNames()) {
				System.out
						.println(name + "=" + prop.getProperty(name));
			}
		} else {
			System.err.println("Error: No file found : ");
		}

	}

}

name=Naveen
country=US

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s