XML-based configuration files

C

Chris

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?
 
J

Jason.Herald

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
 
S

Stefan Ram

Chris said:
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

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.)
 
O

ownowl

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
 
L

Lew

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/a...ToXML(java.io.OutputStream, java.lang.String)>
and
<http://java.sun.com/javase/6/docs/a...utStream, java.lang.String, java.lang.String)>

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

Thomas Kellerer

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
 
E

EricF

Chris a écrit :

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
 
C

Chris

Lew said:
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/a...ToXML(java.io.OutputStream, java.lang.String)>

and
<http://java.sun.com/javase/6/docs/a...utStream, java.lang.String, java.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.
 
C

Chris

Stefan said:
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



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.
 
C

Chris

EricF said:
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.
 
L

Lew

Chris said:
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. :)
 
L

Lew

Lew said:
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.
 
L

Lew

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.
 
W

Wildemar Wildenburger

Lew said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top