aspose file tools*
The moose likes Beginning Java and the fly likes Creating my first config file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating my first config file" Watch "Creating my first config file" New topic
Author

Creating my first config file

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I'd like to go back through some of the programs I have written in the past that no longer work and update them. They stopped working because I never use config files and all the settings are hard coded into the class files. I realize this is a terrible idea and I would like to fix it. I've decided to use an XML file approach because they are easy to view in a web browser, there are lots of tools that can make use of them, etc.

My question is how to read this XML file into my code and then use the data? Yes, I know how to read a file into memory, I could also split the tags into cookies or whatever, but XML is so popular I figured there is something built into Java especially for XML file reading. I would like to add this functionality to my old stuff with as little impact as possilbe to the code itself since this will only be ran once when the program first loads.

Any design recomendations or XML file reading libraries, tools, objects, etc would be greatly appreciated. Also, I think this is a beginner question but if not feel free to move.

Thanks JavaRanchers!

SCJA
~Currently preparing for SCJP6
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Brian:

There are several OS XML libraries out there for Java, in addition to the standard Java library: jdom, dom4j, etc., etc., etc. I've found all of them to more or less painful to use. I would actually recommend that you use Groovy to handle your XML, for the following reasons:

1. Groovy handles XML natively
2. You can use Java classes with Groovy, and vice versa
3. Groovy *is* Java under the hood

There are a couple of hoops you have to jump through to get dependencies to compile, but nothing serious.

John.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Thanks John, I've heard "Groovy" tossed around a bit but haven't had any experience with it. I'll have to check it out.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

Yes, I agree. All classes which require you to hack your way through an XML DOM object are tedious to use and break easily if you change things. But there are things like Apache Digester which can be made to convert an XML configuration file into Java objects. There are also full-blown XML-to-Object mapping tools like JAXB, but they are much more heavy-duty than Digester is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating my first config file
 
Similar Threads
Reading from a XML file or from Database ?
Struggling to run a script at regular intervals - any ideas?
Struts application flow
Using Third party tool in Swings
Spring in Action - How to organize Bean Configuration files