Thursday, March 20, 2008

Java : Parsing XML

So there I was needing to parse some XML in a Java program and not wanting to refresh my memory of the Java classes to do this.

Had a look around on the Internet and found JFig

This allows you to set up XML files and then read the parameters using a construct like:

JFig.getInstance().getValue(sectionName, key)

So for XML like:

#section name="Section1"*
#entry key="key1" value="val1" /*


JFig.getInstance().getValue("Section1", "key1")

Neat - and saves me doing the parsing.

But then I discovered some other neat features e.g. you have have a hierarchy of XML files and some "inherit" others.

Also, you have have substitution values e.g.

#entry key="key2" value="[Section1]{key1}" /*

which allows you to define some "constants" at the beginning of the XML file and then JFig will replicate them through for you.

... where # = "<" and * = ">"

Very neat!

Useful article here. Another solution is to use Jakarta Commons Configuration but JFig is much simpler.


No comments: