DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Loading An Xml Properties File

05.02.2007
| 24291 views |
  • submit to reddit
        // simple example of loading an xml file into Properties.

Properties properties = new Properties();
try {
    InputStream xmlStream = getClass().getResourceAsStream("properties.xml");
    if( xmlStream == null ) {
         //throw some error
    }            
    properties.loadFromXML(xmlStream);
} catch (IOException exception) {
    // throw exception
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="plugin.TestPlugin">com.plugin.test.TestPluginModule</entry>
    <entry key="messages.TestPlugin">TestPluginMessages.properties</entry>
</properties>
    

Comments

Snippets Manager replied on Sun, 2007/02/11 - 5:06am

Please note that properties.loadFromXML(xmlStream) is only available Java 5 and up: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#loadFromXML(java.io.InputStream) Earlier Properties did not have such luxury. http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html