XML-based configuration files

Discussion in 'Java' started by Chris, Jan 12, 2008.

  1. Chris

    Chris Guest

    Does anyone have a nice utility class they use to read/write XML-based
    configuration files?

    I've gotten away from Properties files because I often need a simple
    data structure where 1) the values are hierarchical, 2) there can be
    more than one value for a given key, and 3) the order of the values is
    preserved.

    So I wrote my own little replacement for Properties that has those
    features, but I'm not happy with it.

    It would better to use something standard and off-the-shelf. Any
    suggestions?
     
    Chris, Jan 12, 2008
    #1
    1. Advertising

  2. Chris

    Guest

    On Jan 12, 5:17 pm, Chris <> wrote:
    > Does anyone have a nice utility class they use to read/write XML-based
    > configuration files?
    >
    > I've gotten away from Properties files because I often need a simple
    > data structure where 1) the values are hierarchical, 2) there can be
    > more than one value for a given key, and 3) the order of the values is
    > preserved.
    >
    > So I wrote my own little replacement for Properties that has those
    > features, but I'm not happy with it.
    >
    > It would better to use something standard and off-the-shelf. Any
    > suggestions?


    The java.util.Properties class will read in properties in xml. I
    found this resource which I hope helps:
    http://www.ibm.com/developerworks/java/library/j-tiger02254.html
     
    , Jan 12, 2008
    #2
    1. Advertising

  3. Chris

    Stefan Ram Guest

    Chris <> writes:
    >I've gotten away from Properties files because I often need a
    >simple data structure where 1) the values are hierarchical, 2)
    >there can be more than one value for a given key, and


    In XML, multiple value per key are not actually allowed,
    this would be

    <example key=value key=value1 ...></example>

    They only might be simulated by direct subelements.

    In Unotal, they are allow directly, and would look like

    < &example key=value key=value1 ... >

    >3) the order of the values is preserved.


    In Unotal, it is not preserved, because all values are
    deemed to form a set.

    However, one can use a list of values as in

    < &example key=< value value1 >>

    This would preserve the sequence.

    The Java-Implementation of Unotal is available as
    a GPL library and is being described in

    http://www.purl.org/stefan_ram/pub/junotal_tutorial

    (To compile this library from the sources, currently about
    three occurences of »private« needs to be removed in the
    source as indicated by compiler error messages.)
     
    Stefan Ram, Jan 12, 2008
    #3
  4. Chris

    ownowl Guest

    Chris a écrit :
    > Does anyone have a nice utility class they use to read/write XML-based
    > configuration files?
    >
    > I've gotten away from Properties files because I often need a simple
    > data structure where 1) the values are hierarchical, 2) there can be
    > more than one value for a given key, and 3) the order of the values is
    > preserved.
    >
    > So I wrote my own little replacement for Properties that has those
    > features, but I'm not happy with it.
    >
    > It would better to use something standard and off-the-shelf. Any
    > suggestions?


    Perhaps you could use the xstream library, that bind automatically xml
    file with java objects. I think your three constraints should be
    respected with this usefull library
     
    ownowl, Jan 12, 2008
    #4
  5. Chris

    Lew Guest

    wrote:
    > On Jan 12, 5:17 pm, Chris <> wrote:
    >> Does anyone have a nice utility class they use to read/write XML-based
    >> configuration files?
    >>
    >> I've gotten away from Properties files because I often need a simple
    >> data structure where 1) the values are hierarchical, 2) there can be
    >> more than one value for a given key, and 3) the order of the values is
    >> preserved.
    >>
    >> So I wrote my own little replacement for Properties that has those
    >> features, but I'm not happy with it.
    >>
    >> It would better to use something standard and off-the-shelf. Any
    >> suggestions?

    >
    > The java.util.Properties class will read in properties in xml. I
    > found this resource which I hope helps:
    > http://www.ibm.com/developerworks/java/library/j-tiger02254.html


    It's positively *amazing* how useful Javadocs can be!

    I found this *incredible* reference about Properties and XML at
    <http://java.sun.com/javase/6/docs/api/java/util/Properties.html>
    !

    It goes into a little moredetail at
    <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#loadFromXML(java.io.InputStream)>
    and
    <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#storeToXML(java.io_OutputStream,%20java.lang.String)>
    and
    <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#storeToXML(java.io_OutputStream,%20java.lang.String,%20java.lang.String)>

    It's a wonder how much information one can glean from the documentation!

    --
    Lew
     
    Lew, Jan 12, 2008
    #5
  6. Chris wrote on 12.01.2008 23:17:
    > Does anyone have a nice utility class they use to read/write XML-based
    > configuration files?
    >
    > I've gotten away from Properties files because I often need a simple
    > data structure where 1) the values are hierarchical, 2) there can be
    > more than one value for a given key, and 3) the order of the values is
    > preserved.
    >
    > So I wrote my own little replacement for Properties that has those
    > features, but I'm not happy with it.
    >
    > It would better to use something standard and off-the-shelf. Any
    > suggestions?


    I use XMLDecoder and XMLEncoder to write my (configuration) objects to an xml file.

    <http://java.sun.com/j2se/1.5.0/docs/api/java/beans/XMLDecoder.html>
    <http://java.sun.com/j2se/1.5.0/docs/api/java/beans/XMLEncoder.html>

    Thomas
     
    Thomas Kellerer, Jan 12, 2008
    #6
  7. Chris

    EricF Guest

    In article <47894576$0$21150$-internet.fr>, ownowl <> wrote:
    >Chris a écrit :
    >> Does anyone have a nice utility class they use to read/write XML-based
    >> configuration files?
    >>
    >> I've gotten away from Properties files because I often need a simple
    >> data structure where 1) the values are hierarchical, 2) there can be
    >> more than one value for a given key, and 3) the order of the values is
    >> preserved.
    >>
    >> So I wrote my own little replacement for Properties that has those
    >> features, but I'm not happy with it.
    >>
    >> It would better to use something standard and off-the-shelf. Any
    >> suggestions?

    >
    >Perhaps you could use the xstream library, that bind automatically xml
    >file with java objects. I think your three constraints should be
    >respected with this usefull library


    I hate all the XML configuration needed these days. If you can avoid xml it's
    a good thing.

    Sometimes you need XMLl. I agree, xstream is great for serializing/deserialing
    xml to Java.

    Eric
     
    EricF, Jan 13, 2008
    #7
  8. Chris

    Chris Guest

    Lew wrote:
    > wrote:
    >> On Jan 12, 5:17 pm, Chris <> wrote:
    >>> Does anyone have a nice utility class they use to read/write XML-based
    >>> configuration files?
    >>>
    >>> I've gotten away from Properties files because I often need a simple
    >>> data structure where 1) the values are hierarchical, 2) there can be
    >>> more than one value for a given key, and 3) the order of the values is
    >>> preserved.
    >>>
    >>> So I wrote my own little replacement for Properties that has those
    >>> features, but I'm not happy with it.
    >>>
    >>> It would better to use something standard and off-the-shelf. Any
    >>> suggestions?

    >>
    >> The java.util.Properties class will read in properties in xml. I
    >> found this resource which I hope helps:
    >> http://www.ibm.com/developerworks/java/library/j-tiger02254.html

    >
    > It's positively *amazing* how useful Javadocs can be!
    >
    > I found this *incredible* reference about Properties and XML at
    > <http://java.sun.com/javase/6/docs/api/java/util/Properties.html>
    > !
    >
    > It goes into a little moredetail at
    > <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#loadFromXML(java.io.InputStream)>
    >
    > and
    > <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#storeToXML(java.io_OutputStream,%20java.lang.String)>
    >
    > and
    > <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#storeToXML(java.io_OutputStream,%20java.lang.String,%20java.lang.String)>
    >
    >
    > It's a wonder how much information one can glean from the documentation!
    >


    The Properties object is unsuitable because it doesn't meet the
    requirements detailed above: hierarchical, multivalued, order-preserving.
     
    Chris, Jan 13, 2008
    #8
  9. Chris

    Chris Guest

    Stefan Ram wrote:
    > Chris <> writes:
    >> I've gotten away from Properties files because I often need a
    >> simple data structure where 1) the values are hierarchical, 2)
    >> there can be more than one value for a given key, and

    >
    > In XML, multiple value per key are not actually allowed,
    > this would be
    >
    > <example key=value key=value1 ...></example>
    >
    > They only might be simulated by direct subelements.
    >
    > In Unotal, they are allow directly, and would look like
    >
    > < &example key=value key=value1 ... >
    >
    >> 3) the order of the values is preserved.

    >
    > In Unotal, it is not preserved, because all values are
    > deemed to form a set.
    >
    > However, one can use a list of values as in
    >
    > < &example key=< value value1 >>
    >
    > This would preserve the sequence.
    >
    > The Java-Implementation of Unotal is available as
    > a GPL library and is being described in
    >
    > http://www.purl.org/stefan_ram/pub/junotal_tutorial
    >
    > (To compile this library from the sources, currently about
    > three occurences of »private« needs to be removed in the
    > source as indicated by compiler error messages.)
    >


    Thanks. Unotal looks interesting.

    In my own class, I implement multiple values per key thusly:

    <mykey>myvalue0</mykey>
    <mykey>myvalue1</mykey>
    <mykey>myvalue2</mykey>

    Again, I'm not happy with this approach.
     
    Chris, Jan 13, 2008
    #9
  10. Chris

    Chris Guest

    EricF wrote:
    > In article <47894576$0$21150$-internet.fr>, ownowl <> wrote:
    >> Chris a écrit :
    >>> Does anyone have a nice utility class they use to read/write XML-based
    >>> configuration files?
    >>>
    >>> I've gotten away from Properties files because I often need a simple
    >>> data structure where 1) the values are hierarchical, 2) there can be
    >>> more than one value for a given key, and 3) the order of the values is
    >>> preserved.
    >>>
    >>> So I wrote my own little replacement for Properties that has those
    >>> features, but I'm not happy with it.
    >>>
    >>> It would better to use something standard and off-the-shelf. Any
    >>> suggestions?

    >> Perhaps you could use the xstream library, that bind automatically xml
    >> file with java objects. I think your three constraints should be
    >> respected with this usefull library

    >
    > I hate all the XML configuration needed these days. If you can avoid xml it's
    > a good thing.
    >
    > Sometimes you need XMLl. I agree, xstream is great for serializing/deserialing
    > xml to Java.
    >
    > Eric


    Agreed. Configuration files are a necessary evil.
     
    Chris, Jan 13, 2008
    #10
  11. Chris

    Lew Guest

    Chris wrote:
    > Lew wrote:
    >> wrote:
    >>> On Jan 12, 5:17 pm, Chris <> wrote:
    >>>> Does anyone have a nice utility class they use to read/write XML-based
    >>>> configuration files?
    >>>>
    >>>> I've gotten away from Properties files because I often need a simple
    >>>> data structure where 1) the values are hierarchical, 2) there can be
    >>>> more than one value for a given key, and 3) the order of the values is
    >>>> preserved.
    >>>>
    >>>> So I wrote my own little replacement for Properties that has those
    >>>> features, but I'm not happy with it.
    >>>>
    >>>> It would better to use something standard and off-the-shelf. Any
    >>>> suggestions?
    >>>
    >>> The java.util.Properties class will read in properties in xml. I
    >>> found this resource which I hope helps:
    >>> http://www.ibm.com/developerworks/java/library/j-tiger02254.html

    >>
    >> It's positively *amazing* how useful Javadocs can be!
    >>
    >> I found this *incredible* reference about Properties and XML at
    >> <http://java.sun.com/javase/6/docs/api/java/util/Properties.html>
    >> !
    >>
    >> It goes into a little moredetail at
    >> <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#loadFromXML(java.io.InputStream)>
    >>
    >> and
    >> <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#storeToXML(java.io_OutputStream,%20java.lang.String)>
    >>
    >> and
    >> <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#storeToXML(java.io_OutputStream,%20java.lang.String,%20java.lang.String)>
    >>
    >>
    >> It's a wonder how much information one can glean from the documentation!
    >>

    >
    > The Properties object is unsuitable because it doesn't meet the
    > requirements detailed above: hierarchical, multivalued, order-preserving.


    Two out of three ain't bad. :)

    --
    Lew
     
    Lew, Jan 13, 2008
    #11
  12. Chris

    Lew Guest

    Lew wrote:
    > Chris wrote:
    >> Lew wrote:
    >>> wrote:
    >>>> On Jan 12, 5:17 pm, Chris <> wrote:
    >>>>> Does anyone have a nice utility class they use to read/write XML-based
    >>>>> configuration files?
    >>>>>
    >>>>> I've gotten away from Properties files because I often need a simple
    >>>>> data structure where 1) the values are hierarchical, 2) there can be
    >>>>> more than one value for a given key, and 3) the order of the values is
    >>>>> preserved.
    >>>>>
    >>>>> So I wrote my own little replacement for Properties that has those
    >>>>> features, but I'm not happy with it.
    >>>>>
    >>>>> It would better to use something standard and off-the-shelf. Any
    >>>>> suggestions?
    >>>>
    >>>> The java.util.Properties class will read in properties in xml. I
    >>>> found this resource which I hope helps:
    >>>> http://www.ibm.com/developerworks/java/library/j-tiger02254.html
    >>>
    >>> It's positively *amazing* how useful Javadocs can be!
    >>>
    >>> I found this *incredible* reference about Properties and XML at
    >>> <http://java.sun.com/javase/6/docs/api/java/util/Properties.html>
    >>> !
    >>>
    >>> It goes into a little moredetail at
    >>> <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#loadFromXML(java.io.InputStream)>
    >>>
    >>> and
    >>> <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#storeToXML(java.io_OutputStream,%20java.lang.String)>
    >>>
    >>> and
    >>> <http://java.sun.com/javase/6/docs/api/java/util/Properties.html#storeToXML(java.io_OutputStream,%20java.lang.String,%20java.lang.String)>
    >>>
    >>>
    >>> It's a wonder how much information one can glean from the documentation!
    >>>

    >>
    >> The Properties object is unsuitable because it doesn't meet the
    >> requirements detailed above: hierarchical, multivalued, order-preserving.

    >
    > Two out of three ain't bad. :)


    Actually, properties sure can be hierarchical, using a dot notation as many
    practitioners do:

    upper=first,second,third,fourth
    upper.second=alpha,beta,gamma,delta,epsilon,omega
    upper.sibling=Mercury,Venus,Mars,Ceres,Charon
    upper.sibling.next=Oort,Spiral,Milky,Universe

    Put that in an XML format and Bob's your uncle.

    --
    Lew
     
    Lew, Jan 13, 2008
    #12
  13. Chris

    Lew Guest

    EricF wrote:
    >> I hate all the XML configuration needed these days. If you can avoid
    >> xml it's a good thing.
    >>
    >> Sometimes you need XMLl. I agree, xstream is great for
    >> serializing/deserialing xml to Java.


    Chris wrote:
    > Agreed. Configuration files are a necessary evil.


    Would you rather recode, recompile and redeploy your application each time
    there's a change?

    There is absolutely nothing "evil" about configuration files. It's their lack
    that would be evil.

    --
    Lew
     
    Lew, Jan 13, 2008
    #13
  14. Lew wrote:
    > Chris wrote:
    >> Agreed. Configuration files are a necessary evil.

    >
    > Would you rather recode, recompile and redeploy your application each
    > time there's a change?
    >
    > There is absolutely nothing "evil" about configuration files. It's
    > their lack that would be evil.
    >

    I think Chris argumented from a coding-convenience point of view.
    External config options have to be retrieved and processed and kept in
    sync with the program. Hardcoding stuff often is more convenient,
    because you can "just do it". How about saying "Configuration files are
    a necessary PITA"?

    /W
     
    Wildemar Wildenburger, Jan 13, 2008
    #14
    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. Mark
    Replies:
    0
    Views:
    835
  2. Stephan Koser
    Replies:
    3
    Views:
    677
    Juha Laiho
    Dec 7, 2004
  3. Christofer Dutz
    Replies:
    3
    Views:
    493
    Christofer Dutz
    Dec 9, 2003
  4. Andy
    Replies:
    0
    Views:
    542
  5. Matt
    Replies:
    3
    Views:
    347
    Bob Barrows [MVP]
    Apr 23, 2004
Loading...

Share This Page