tags:

views:

265

answers:

11

Does Java have a built in XML library for generating and parsing documents? If not, which third party one should I be using?

A: 

You could have a look to the javax.xml package, which contains everything you need to work with XML documents in Java...

romaintaz
javax.xml only seems to be interfaces defining an xml library, nothing actually usable
Mike
+2  A: 

Yes. Java contains javax.xml library. You can checkout some samples at Sun's Java API for XML Code Samples.

However, I personally like using JDOM library.

notnoop
javax.xml is an API.
dacracot
+1  A: 

javax.xml package contains Java's native XML solution which is actually a special version of Xerces. You can do what you asked with it, however using 3rd party libraries such as JDOM makes the whole process a lot easier.

Esko
+3  A: 

The Sun Java Runtime comes with the Xerces and Xalan implementations that provide the ability to parse XML (via the DOM and SAX intefaces), and also perform XSL transformations and execute XPath queries.

However, it is better to use the JAXP API to work on XML, since JAXP allows you to not worry about the underlying implementation used (Xerces or Crimson or any other). When you use JAXP, at runtime the JRE will use the service provider it can locate, to perform the needed operations. As indicated previously, Xerces/Xalan will be used since it is shipped with the Sun JRE (not others though), so you dont have to download and install a specific provider (say, a different version of Xerces, or Crimson).

A basic JAXP tutorial can be found in The J2EE 1.4 tutorial (Its from the J2EE tutorial, but it will help).

Do note that the Xerces/Xalan implementations provided by the Sun JRE, will not be found in the org.apache.xerces.* or org.apache.xalan.* packages. Instead, they will be present in the internal com.sun.org.apache.xerces.* and com.sun.org.apache.xalan.* packages.

By the way, JDOM is not an XML parser - it will use the parser provided to it by JAXP in order to provide you with an easier abstraction to work with.

Vineet Reynolds
Xerces and Xalan are libraries.
dacracot
+3  A: 

Yes. It has a two options in the javax.xml package: DOM builds documents in memory, and SAX is an event-based approach.

You may also want to look at JDOM, which is a 3rd party library that offers a combination of the two, and can be easier to use.

ndp
DOM and SAX are implementations. JDom is a library with its own API and unique DOM like API.
dacracot
Actually DOM and SAX would be programming interfaces not implementations.
Vineet Reynolds
+1  A: 

Have a look at JAX-B This is increasingly the "standard" way to do XML processing. Uses Java annotations to simplify the programming model. The reference gives sample code for reading and writing XML.

djna
A: 

Java API for XML Processing (JAXP) is part of standard library JavaSE. JAXP allows you to code against standard interface and lets you pick the parser implementation later if needed.

The Java API for XML Processing, or JAXP for short, enables applications to parse and transform XML documents using an API that is independent of a particular XML processor implementation. JAXP also provides a pluggability feature which enables applications to easily switch between particular XML processor implementations.

eed3si9n
+1  A: 

Java does come with a large set of packages and classes to handle XML. These are part of the Standard Edition JDK, and located under the javax.xml package.

Aside from reading XML and writing it with DOM or SAX, these packages also perform XSL transformations, JAX-B object marshalling and unmarshalling, XPath processing and web services SOAP handling. I advise you to read more about these online in Sun's excellent tutorials.

Yuval
A: 

You can use StAX (streaming API for XML) http://en.wikipedia.org/wiki/StAX http://www.xml.com/pub/a/2003/09/17/stax.html https://sjsxp.dev.java.net/

StAx is optimized to process large xml files, without causing OOM (out of memory) problem :)

janetsmith
A: 

As is said above... Java's SDK now comes with Xerces and Xalan. Xalan only implements version 1.0 of the XSLT API, so if you want 2.0, you should look at Saxon from Michael Kay.

dacracot
+1  A: 

I can't tell you which one to use (few requirements specified, and there are a dozen libraries), but I would seriously consider XOM (here).

Written by Eliotte Rusty Harold, it is quite complete in terms of the XML spec, and generally excellent. I have found it very easy to use. See the link above for Harold's motivation and criticism of other solutions.

Michael Easter
+1 XOM is easier to use, more flexible
peter.murray.rust