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.
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.
Joined: Nov 07, 2008
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.
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.