-2

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

Welbog
  • 59,154
  • 9
  • 110
  • 123

7 Answers7

1

"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

Trade-Ideas Philip
  • 1,067
  • 12
  • 21
1

You could take a look into that list for xml serializers and deserializers.

I would suggest the jdk class XmlEncoder+XmlDecoder, xstream or simple.

Karussell
  • 17,085
  • 16
  • 97
  • 197
1

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

Makkes
  • 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
1

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.

http://argonrain.wordpress.com/2009/10/27/000/

chris
  • 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
1

This is what you need, powerful and easy to use.

Preferences API

helpermethod
  • 59,493
  • 71
  • 188
  • 276
0

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?

FacilityDerek
  • 188
  • 1
  • 1
  • 9
0

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.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440