Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

XML-based configuration files

Thread view: 
Chris - 12 Jan 2008 22:17 GMT
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?
Jason.Herald@gmail.com - 12 Jan 2008 22:28 GMT
> Does anyone have a nice utility class they use to read/write XML-based
> configuration files?
[quoted text clipped - 9 lines]
> 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
Lew - 12 Jan 2008 23:19 GMT
>> Does anyone have a nice utility class they use to read/write XML-based
>> configuration files?
[quoted text clipped - 13 lines]
> 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!

Signature

Lew

Chris - 13 Jan 2008 19:11 GMT
>>> Does anyone have a nice utility class they use to read/write XML-based
>>> configuration files?
[quoted text clipped - 30 lines]
>
> 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.
Lew - 13 Jan 2008 19:39 GMT
>>>> Does anyone have a nice utility class they use to read/write XML-based
>>>> configuration files?
[quoted text clipped - 33 lines]
> 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.  :-)

Signature

Lew

Lew - 13 Jan 2008 19:42 GMT
>>>>> Does anyone have a nice utility class they use to read/write XML-based
>>>>> configuration files?
[quoted text clipped - 35 lines]
>
> 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.

Signature

Lew

Stefan Ram - 12 Jan 2008 22:40 GMT
>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.)
Chris - 13 Jan 2008 19:17 GMT
>> I've gotten away from Properties files because I often need a
>> simple data structure where 1) the values are hierarchical, 2)
[quoted text clipped - 30 lines]
>   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.
ownowl - 12 Jan 2008 22:55 GMT
Chris a écrit :
> Does anyone have a nice utility class they use to read/write XML-based
> configuration files?
[quoted text clipped - 9 lines]
> 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
EricF - 13 Jan 2008 06:54 GMT
>Chris a écrit :
>> Does anyone have a nice utility class they use to read/write XML-based
[quoted text clipped - 14 lines]
>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
Chris - 13 Jan 2008 19:23 GMT
>> Chris a écrit :
>>> Does anyone have a nice utility class they use to read/write XML-based
[quoted text clipped - 21 lines]
>
> Eric

Agreed. Configuration files are a necessary evil.
Lew - 13 Jan 2008 20:35 GMT
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.

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

Signature

Lew

Wildemar Wildenburger - 13 Jan 2008 20:51 GMT
>> Agreed. Configuration files are a necessary evil.
>
[quoted text clipped - 3 lines]
> 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
Thomas Kellerer - 12 Jan 2008 23:27 GMT
Chris wrote on 12.01.2008 23:17:
> Does anyone have a nice utility class they use to read/write XML-based
> configuration files?
[quoted text clipped - 9 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.