tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > Properties > Load XML Properties

Load XML Properties 

This example shows loading properties from a XML file,

File Name  :  
com/bethecoder/tutorials/utils/props/LoadXMLProps.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.utils.props;

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

public class LoadXMLProps {
  /**
   @param args
   */
  public static void main(String[] args) {

    Properties prop = new Properties();
    String propFile = "C:/prop_xml_file.xml";
    
    try {
      System.out.println("Before properties load : " + prop);
      prop.loadFromXML(new FileInputStream(propFile));
      System.out.println("After properties load : " + prop);
      
      //Access properties
      System.out.println(prop.getProperty("ONE"));
      System.out.println(prop.getProperty("TWO"));

    catch (InvalidPropertiesFormatException e) {
      e.printStackTrace();
    catch (FileNotFoundException e) {
      e.printStackTrace();
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}
   

XML Properties file format is shown below,
File Name  :  
source/com/bethecoder/tutorials/utils/props/prop_xml_file.xml 

It gives the following output,
Before properties load : {}
After properties load : {TWO=222222, ONE=111111, FOUR=444444, THREE=333333}
111111
222222


                    Print

   



Books Recommendation

 
 
bl  br