Loading properties from XML file
17 May 2008In 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")); } }
Related Posts:
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