tags:

views:

661

answers:

7

I've been using JDOM for general XML parsing for a long time, but get the feeling that there must be something better, or at least more lightweight, for Java 5 or 6.

There's nothing wrong with the JDOM API, but I don't like having to include Xerces with my deployments. Is there a more lightweight alternative, if all I want to do is read in an XML file or write one out?

+1  A: 

We use JAXB - it generates the classes based on the schema. You can generate your files without a schema, and just annotate how you want the xml to be.

David Rabinowitz
I wouldn't call JAXB a replacement for JDOM, though, it solves rather different problems.
skaffman
From the question I understood that a specific XML is need to be read and written. If so, why not to define a schema and save all the tedious parsing. If an arbitrary xml is need to be written then I agree (I believe is is quite a rare case)
David Rabinowitz
A: 

You should check out Commons Digester (see the answer I've given here). It provides a very lightweight mechanism for parsing XML.

Adamski
+4  A: 

The best lightweight alternative is, in my opinion, XOM, but JDOM is still a very good API, and I see no reason to replace it.

It doesn't have a dependency on Xerces, though (at least, it doesn't need the Apache Xerces distro, it works alongside the Xerces that's packaged into the JRE).

skaffman
+3  A: 

I've used the javax.xml.stream package (XMLStreamReader/XMLStreamWriter) to read and write XML using xml pull/push techniques. It's worked for me so far.

KitsuneYMG
+1 with the note that the javax.xml.stream package is only available in Java 6.
Adam Paynter
If you don't have java 6 but still want to use xmlpp you can check out http://www.xmlpull.org/ . The javax.stream package is NOT an xmlpull.org compatible pull parser (it's API is slightly different) but they are very close.
KitsuneYMG
Do you know of any 3rd party streaming APIs suitable for *writing* XML?
Adam Paynter
@Adamhttp://www.xmlpull.org/v1/download/unpacked/doc/quick_write.htmlxmlpull is reading and writing.
KitsuneYMG
@kts: That's convenient! I guess the "pull" threw me off
Adam Paynter
A: 

JDOM is very good and simple. There has been many new ways to parse XML after release of JDOM, but those has have different focus than simplicity. JAXB makes things simple in some cases when you have well known XML document has your schema does not get updated daily basis. New push parsers are very good and even mandatory for very large XML files (hundreds of MBs). Speed benefit for SAX parser can be ten fold.

tomtom
A: 

Use one of the XML APIs that come standard with Java, so that you don't have to include any third-party libraries.

XML in the Java Platform Standard Edition (Java SE) 6

Jesper
A: 

I would like to think JAXP is a good choise for you. It's standard, included in JDK, it provides clear interface and allows to hook up any implementations.. If all what you need in is to read and write not very large and overcomplicated xml files, JAXP DOM api embedded in JDK will cover you requirements.

Zorkus