Ben
Ben

Reputation: 62404

Where can I find Java EE 6 XML Libraries

I'm learning Java EE 6 & Jax-RS (haven't started anything about Jax-RS yet) to build an xml api for my work. I have my java controllers setup and I'm looking now to use my models to generate XML output. I can't seem to find any XML libraries by searching Google. Can someone help point me in the right direction?

Upvotes: 2

Views: 381

Answers (4)

GeertPt
GeertPt

Reputation: 17854

If you use JAX-RS, you can use @Produces("application/xml"), then you will have an xml api. JAX-RS works by default with JAXB.

See http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e318

Upvotes: 5

millimoose
millimoose

Reputation: 39950

Java SE already includes XML parsers in three styles:

  1. SAX – You create one using the SAXParserFactory helper class. I believe it's the oldest of the APIs and it follows a W3C standard. It's also fairly difficult to use because it's used by passing a bunch of callback for the parser to invoke you have to keep track of where you are in the document yourself, and with StAX being available there's pretty much no reason to use it. It also can't be used to write XML.
  2. DOM – You create a parser using a DocumentBuilderFactory. The only one of the APIs to offer random access to the parsed three. It's also the slowest and uses the most memory, so it's not recommended if you need to handle huge documents. (Should be fine to serialise web service output though.) Java's implementation follows the W3C standard which makes the API a little confusing since it refrains from following Java idioms. There's two fairly popular alternative DOM-style APIs that are more Java-like, JDOM and dom4j.
  3. StAX – the new kid on the block, Java-specific, and my personal favourite. It works by letting you advance through a stream of events. This makes it much faster than DOM while much easier to wrap your head around. You start by using XMLInputFactory to read XML and XMLOutputFactory to write it.

If you're going to use JAX-RS, you probably don't want to use these "low-level" XML libraries. JAX-RS should handle parsing requests and formatting responses for you. I believe it uses JAXB for this, also a part of the standard library. JAXB will serialise a Java object automatically based on annotations on the class and its fields / properties that control the serialisation.

Upvotes: 3

Chip McCormick
Chip McCormick

Reputation: 744

The libraries aren't JavaEE specific. For generating XML, StAX and JDOM are the most commonly used in my experience. StAX is part of the JDK. They also do XML parsing and validation.

A couple of tutorials: 1 2

StAX uses an event based model (read the document and when you see element X do something) and unsuprisingly JDOM uses syntax similar to DOM (find element X.Y). JDOM loads the entire document tree into memory, which eats up memory but performs faster.

Here's an overview comparing the two tools and JAXB.

Upvotes: 1

Antti Kolehmainen
Antti Kolehmainen

Reputation: 1091

Personally I like the simplicity of Simple XML http://simple.sourceforge.net/

Also you could consider using JAXB that comes with Java EE6. http://www.oracle.com/technetwork/articles/javase/index-140168.html

Upvotes: 1

Related Questions