Is there an easy way to read an XML file in Java? [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this questionI'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...
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/
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
You could take a look into that list for xml serializers and deserializers.
I would suggest the jdk class XmlEncoder+XmlDecoder, xstream or simple.
This is what you need, powerful and easy to use.
Preferences API
"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
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?
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.
精彩评论