Read XML Properties in Java

Once in a while you need to externalize some configuration without the overhead of a complete framework, here’s a simple method to read an XML formatted property file in java. In most cases, it’s a performance advantage to wrap this up in a Singleton pattern, but that’s a different topic altogether.

private getAttributes() {
final String filename = "";
final InputStream input = getClass().getClassLoader().getResourceAsStream(filename);
System.err.println("Cannot find properties:"+ filename);
final java.util.Properties props = new java.util.Properties();
try {
hostprop = props.getProperty("hostname",null);
userprop = props.getProperty("username",null);
pswdprop = props.getProperty("password",null);
catch(final Exception e){
System.err.println("Error occurred while reading properties file:"+ input);
try {
catch(final ex){

The matching file would resemble…

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE properties SYSTEM "">
<entry key="hostname">localhost</entry>
<entry key="username">example</entry>
<entry key="password">example</entry>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.