I'm fairly new to Java and am writing an app that needs an XML config file. The problem I have is that there doesn't seem to be any easy way to do this, which seems a bit strange. I've looked SAX and DOM and both seem quite complicated. Are there any other good API's out there? What's the best way to do this in Java? Thanks...
-
2Many dupes. Here's a recent one with a nicely formatted question: http://stackoverflow.com/questions/2333479/how-to-read-an-xml-file-with-java – kdgregory Mar 27 '10 at 11:36
-
There are many XML api to read XML, see this link http://stackoverflow.com/questions/373833/best-xml-parser-for-java – vtd-xml-author Mar 27 '10 at 18:53
-
Yes, it is easy if you already learned the api. – peterh Sep 08 '15 at 00:40
7 Answers
"I'm fairly new to Java ... there doesn't seem to be any easy way to do this, which seems a bit strange." This is an old post. Now that you've been using Java for 4 years, are you still surprised when it's hard to do simple things? :)
I started with the JDK, and built my own XmlHelper class on top of it. It's small and simple. Every time I find myself writing the same code over and over, it gets copied into that class. It's made my life a lot easier. http://marketmovers.blogspot.com/2014/02/the-easy-way-to-read-xml-in-java.html

- 1,067
- 12
- 21
I would recommend the Commons Configuration library: http://commons.apache.org/configuration/index.html Take a look at the HOWTOs to see how easy it is to get some information from an XML file.
All other libs I know involve either operating on the DOM directly or registering handlers for SAX parsing (which both involve a high overhead of code). JAXB is also an alternative but doesn't involve less overhead code than the former two.
Max

- 1,768
- 15
- 19
-
Yeah, and of course: You should consider using sth. other than XML if you don't really want to take advantage of the XML capabilities. Properties files are the simplest of the alternatives. – Makkes Mar 27 '10 at 11:28
I've written a very simple API for precisely this reason. It uses the DOM parser underneath, but exposes a very simple and easy-to-use API that allows you to get to the XML data really easily. It's just a single Java file that you can use as a library in your code. Hope that helps.

- 11
- 1
-
Thanks all for your replies. Commons Configuration looks quite good and seems pretty powerful, however, in the end I decided to use this solution in my app. The only negative thing about Commons Configuration is that it's quite heavyweight for just a simple bit of config. This solution is really lightweight and does everything I need. Thanks a lot for the link! – Mar 27 '10 at 18:54
SAX and DOM are the "Standard" ways to approach parsing an XML file in Java.
There are alternatives to XML too.
Perhaps all you need is a properties file ?
Have you considered JSON?

- 188
- 1
- 1
- 9
A nice alternative to parsing the file yourself (especially for configuration purposes) is to use Apache Commons Digester.
You simply nominate what parts of the XML file will trigger which setters/methods you require, and Digester will do the rest. You can build arbitrarily complex configurations and the 'parsing' code remains trivial.
e.g.
digester.addObjectCreate( "addresses/address", Address.class );
digester.addBeanPropertySetter( "addresses/address/addressLine1", "addressLine1" );
creates an Address object and sets the first line of the address. See this tutorial for more info.
Alternatively XStream offers (possibly) the simplest XML-to-object conversion available in Java. To create an instance of a (say) Configuration
class from an input stream is a one-line operation.

- 268,207
- 37
- 334
- 440