View RSS Feed

My Java Tips

Loading properties from XML file

Rate this Entry
by , 11-07-2011 at 06:58 PM (1240 Views)
In this post, I will present how to read properties from XML files.




We have following XML file with the required properties:

<!--?xml version="1.0" encoding="UTF-8"?-->
<properties>
<comment>Testing</comment>
<entry key="mode">alpha</entry>
<entry key="entertaionment">movie</entry>
</properties>
DTD for the XML file is as follows:

<!--?xml version="1.0" encoding="UTF-8"?-->
<!-- DTD for properties -->
<!--ELEMENT properties ( comment?, entry* ) -->
<!--ATTLIST properties version CDATA #FIXED "1.0"-->
<!--ELEMENT comment (#PCDATA) -->
<!--ELEMENT entry (#PCDATA) -->
<!--ATTLIST entry key CDATA #REQUIRED-->

The Java code to read the properties is given below:

Java Code:
import java.util.*;
import java.io.*;

public class LoadSampleXML {
  public static void main(String args[]) throws Exception {
    Properties prop = new Properties();
    FileInputStream fis =
      new FileInputStream("sampleprops.xml");
    prop.loadFromXML(fis);
    prop.list(System.out);
    System.out.println("nThe mode property: " +
        prop.getProperty("mode"));
  }
}

Submit "Loading properties from XML file" to Facebook Submit "Loading properties from XML file" to Digg Submit "Loading properties from XML file" to del.icio.us Submit "Loading properties from XML file" to StumbleUpon Submit "Loading properties from XML file" to Google

Tags: None Add / Edit Tags
Categories
XML

Comments