Load/Read XML Configuration Files Using Apache Digester

There would no developer in this world who haven’t heard or used XML in his daily usage. When it really comes to developing a new product , Creating a Soap message or Communicating over ESB we always have to think about XML. Here i’m providing an efficient approach other then SAX and DOM that can easily setup ypur project configuration in Minutes by creating configuration objects from xml

Apache Digestor is One such utility which was create for a purpose to make XML to Dataobject Conversion very easy and straight forward

Digestor as the name suggests consumes XML and Created Javaobjects which represent the Configuration

Code that can do this is pretty simple and straight forward

/**
*
*/
package com.linkwithweb.configuration.utilities;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;

import com.linkwithweb.configuration.dataobject.ReplayMagix;

/**
* @author ashwin kumar
*
*/
public class ReplayMagixDigestor {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ReplayMagixDigestor xmlDigest = new ReplayMagixDigestor();
xmlDigest.digest();
}

public void digest() {
try {
// Create Digester using rules defined in academyRules.xml
Digester digester = DigesterLoader.createDigester(this.getClass()
.getClassLoader().getResource("replayRules.xml"));

// Parse academy.xml using the Digester to get an instance of
// Academy
ReplayMagix a = (ReplayMagix) digester.parse(this.getClass()
.getClassLoader().getResourceAsStream("replay-magix.xml"));

System.out.println(a.getClasspath().getPathElements().size());

System.out.println(a.getClasspath().getPathElements().get(0)
.getLocation());

System.out.println(a.getDebugPoints().get(0).getPoint());
} catch (Exception e) {
e.printStackTrace();
}
}

}

As you see in the code there is a XML which i want to load into configuration object and there is another xml which defines how i should load .

My Sample XML Definition is (replay-magix.xml)

<?xml version=”1.0″?>
<replaymagix name=”TEST”>
<classpath>
<pathelement location=”lib/” />
<pathelement path=”lib2/test.jar” />
</classpath>

<debug-point>
<classname></classname>
<interfacename></interfacename>
<parentname></parentname>
<methodname></methodname>
<point>before/after</point>
</debug-point>
</replaymagix>

And Here is my Rule which defines how to Load it (replayRules.xml)

<?xml version=”1.0″?>
<digester-rules>
<pattern value=”replaymagix”>
<object-create-rule
classname=”com.linkwithweb.configuration.dataobject.ReplayMagix” />
<set-properties-rule />
<pattern value=”classpath”>
<object-create-rule
classname=”com.linkwithweb.configuration.dataobject.Classpath” />

<pattern value=”pathelement”>
<object-create-rule
classname=”com.linkwithweb.configuration.dataobject.PathElement” />
<set-properties-rule />
<set-next-rule methodname=”addPathElement” />
</pattern>

<set-next-rule methodname=”setClasspath” />
</pattern>

<pattern value=”debug-point”>
<object-create-rule
classname=”com.linkwithweb.configuration.dataobject.DebugPoint” />
<bean-property-setter-rule pattern=”classname” />
<bean-property-setter-rule pattern=”interfacename” />
<bean-property-setter-rule pattern=”parentname” />
<bean-property-setter-rule pattern=”methodname” />
<bean-property-setter-rule pattern=”point” />

<set-next-rule methodname=”addDebugPoint” />
</pattern>
</pattern>
</digester-rules>

If you need working and mavenized code contact ashwin@linkwithweb.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s