Best way to read an XML properties file?

Discussion in 'Java' started by Bret Schuhmacher, Jul 10, 2007.

  1. 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
     
    Bret Schuhmacher, Jul 10, 2007
    #1
    1. Advertising

  2. Bret Schuhmacher

    Stefan Ram Guest

    "Bret Schuhmacher" <> writes:
    >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.
     
    Stefan Ram, Jul 10, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ESPN Lover

    Which way is best to read from a file?

    ESPN Lover, Oct 12, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    397
    ESPN Lover
    Oct 13, 2004
  2. Stan
    Replies:
    3
    Views:
    497
    Brock Allen
    May 5, 2005
  3. Alan Silver
    Replies:
    8
    Views:
    3,994
    Scott Allen
    Dec 8, 2005
  4. Karthikesh Raju

    best way to read a configuration file

    Karthikesh Raju, Mar 3, 2004, in forum: Python
    Replies:
    5
    Views:
    457
    Kevin Dahlhausen
    Mar 3, 2004
  5. Raymond Schanks
    Replies:
    0
    Views:
    353
    Raymond Schanks
    Jul 15, 2010
Loading...

Share This Page