tags:

views:

547

answers:

9
+6  A: 

Since you want to parse config files, I think commons-configuration would be the best solution.

Commons Configuration provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources (including XML)

Bozho
could you please more explain about what commons-configuration is?
Yatendra Goel
I linked it and provided a quote. Go an read the user guide, it's straightforward.
Bozho
I tried commons-cofiguration and got one compiler error as shown above in the snapshot... The snapshot may be too unclear due to size reduction so you can access the snapshot at http://i45.tinypic.com/2eamoeg.jpg ............. OR ..... just drag n drop the pic to your browser address bar (if you are using firefox)
Yatendra Goel
Yatendra Goel you have to download apache commons-lang as well. And some other commons as well - see here http://commons.apache.org/configuration/dependencies.html
Bozho
I wrote the following statement but it is not working..... HOST = config.getString("db-host");...................... Is there any mistake in this statement...
Yatendra Goel
The above statement is written w.r.t the config.xml shown above.
Yatendra Goel
how it isn't working? exception, null?
Bozho
it is returning null
Yatendra Goel
http://commons.apache.org/configuration/userguide/howto_xml.html#Accessing_properties_defined_in_XML_documents see here
Bozho
+1  A: 

There are several XML parsers for Java. One I've used and found particularly developer friendly is JDOM. And by developer friendly, I mean "java oriented" (i.e., you work with objects in your program), instead of "document oriented", as some other tools are.

dariopy
+1  A: 

You could use a simple DOM parser to read the xml representation.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse("config.xml");
Luhar
+7  A: 

I like jdom:

SAXBuilder parser = new SAXBuilder();
Document docConfig = parser.build("config.xml");
Element elConfig = docConfig.getRootElement();
String host = elConfig.getChildText("host");
Llistes Sugra
A: 

For a similar use case in my application I used JaxB. With Jaxb, reading XML files is like interacting with Java POJOs. But to use JAXB you need to have the xsd for this xml file. You can look for more info here

Cshah
+2  A: 

I would recommend Commons Digester, which allows you to parse a file without writing reams of code. It uses a series of rules to determine what action is should perform when encountering a given element or attribute (a typical rule might be to create a particular business object).

Adamski
A: 

If you want to be able to read and write objects to XML directly, you can use XStream

Jorn
A: 

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
Hi, can't you stick to just one user account? http://stackoverflow.com/users/303147/chris Register yourself.
BalusC