Loading properties from XML file

17 May 2008

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"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<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:

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"));
  }
}

del.icio.us:Loading properties from XML file  digg:Loading properties from XML file  spurl:Loading properties from XML file  wists:Loading properties from XML file  simpy:Loading properties from XML file  newsvine:Loading properties from XML file  blinklist:Loading properties from XML file  furl:Loading properties from XML file  reddit:Loading properties from XML file  fark:Loading properties from XML file  blogmarks:Loading properties from XML file  Y!:Loading properties from XML file  smarking:Loading properties from XML file  magnolia:Loading properties from XML file  segnalo:Loading properties from XML file  gifttagging:Loading properties from XML file

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Loading properties from XML file

Leave a Reply