Hi all,
I hope all you Java gurus that have been around the block a time or two can
answer this simple question.
What's the easiest way to read an XML properties file of relative
complexity? I know about Properties.readFromXML, but I don't think it'll
work on my semi-complex property file.
Here's my config file:
<DW>
<dir name="c:\x\y" subdirs="true">
<mask value="*.go">
<condition value="-*.go"/>
<operation value="encrypt"/>
<key value="key1"/>
<iv value="1234123412341234"/>
<mode value="AES/CBC/PKCS5Padding"/>
<etluser>
<etlpassword>
</mask>
<mask value="txt">
<condition value=">100k"/>
<operation value="encrypt"/>
<key value="key2"/>
<iv value="1234123413241234"/>
<mode value="AES/CBC/PKCS5Padding"/>
<etluser>
<etlpassword>
</mask>
</dir>
<dir name="c:\a\b\c" subdirs="true">
<mask value="*.txt">
<operation value="encrypt"/>
<key value="key3"/>
<iv value="6789678967896789"/>
<mode value="AES/CBC/PKCS5Padding"/>
<etluser>
<etlpassword>
</mask>
<mask value="*.enc">
<operation value="decrypt"/>
<key value="key4"/>
<iv value="1234123412341234"/>
<mode value="AES/CBC/PKCS5Padding"/>
<etluser>
<etlpassword>
</mask>
</dir>
</DW>
This is for a "DirectoryWatcher" program that will encrypt/decrypt files
that appear in the directories. I say it's semi-complex because the "dir"
element will have multiple occurrences, forming a list. So, too, does the
mask element - there are many masks to a "dir" element that I need brought
into the program as a list so I can iterate over the values.
I found http://easyconf.sourceforge.net. Are there other, better ways of
getting the values out of an XML config file other than parsing the doc
using DOM4J or equivalent?
Thanks,
Bret
Stefan Ram - 10 Jul 2007 15:57 GMT
>What's the easiest way to read an XML properties file of
>relative complexity?
This does not answer your question, but I have developed a
special notation for complex properties and now would like to
take this opportunity to advertise this notation and a GPL
library implementing it.
For simple cases, my DOM implements the Map and List interface:
roomFrom( "< a=b c=d x y z >" ).get( "a" )
gives »b«, and
roomFrom( "< a=b c=d x y z >" ).get( 0 )
gives »x«, assuming a preceding
import static de.dclj.ram.notation.unotal.RoomFromModule.roomFrom;
A java.io.File can be used instead of a string
so as to read the configuration from a file.
A slightly more complex example:
public class Main
{ public static void main( final java.lang.String[] args )
{
final de.dclj.ram.notation.unotal.RoomSource room =
de.dclj.ram.notation.unotal.RoomFromModule.roomFrom
(
" " +
" < < &car " +
" Chevy=< doors=4 paint=green > " +
" Ford=< doors=2 paint=purple > " +
" Nissan=< doors=3 paint=red >> " +
" < &bike " +
" < inch=26 &green > " +
" < inch=27 &purple >>> " +
" " );
java.lang.System.out.println
( room.getRoom( 0 ).getRoom( "Ford" ).get( "doors" ));
java.lang.System.out.println
( room.getRoom( 0 ).getType() );
java.lang.System.out.println
( room.getRoom( 1 ).hasType( "bike" ));
for( final java.lang.Object o : room.getRoom( 1 ))
java.lang.System.out.println( o ); }}
2
car
true
< &green inch =26 >
< &purple inch =27 >
The notation is being described in
http://www.purl.org/stefan_ram/pub/unotal_en
The library is here
http://www.purl.org/stefan_ram/pub/ram-jar
Disclaimer: The recent jar contains some debug code and is
experimental. If someone would want to use this, I can provide
a jar without the debug code and some support.