views:

80

answers:

3

Currently using Java's built-in XML DOM libraries for creation/parsing. Ugly, even with lots of helper classes and methods, and performance and memory usage sucks.

What's the best Java tool or framework for dealing with XML in regards to producing and consuming REST services?

A service I use uses JAXB. I was able to use their classes and so conveniently and easily consume their services. It was beautiful, but JAXB is still quite a pain in most circumstances. So I looked at StAX and VTD-XML. StAX hasn't been updated in about 4 years. Is VTD-XML the state of the art in XML processing in Java?

+4  A: 

You should be able to write JAXB annotated classes, Jersey supports these out of the box and automatically parses them for you. It might not be the most performant solution but is makes for a really nice clean app.

Unless you can prove you have a performance problem (and can trace it to JAXB) then I would't worry about the marshalling/unmarshalling overhead.

Edit:
The JAXB annotated classes can be really simple. Modelling XML like;

<customer>
  <name>Fred</name>
  <email>[email protected]</email>
  <id>12345</id>
</customer>

is as easy as this;

@XmlRootElement
public class Customer
{
  private String name;
  private String email;
  private long id;

  public Customer()
  {
  }

  //getters and setters
}

You can then write services like this;

@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/getCustomer")
public Customer getCustomer()
{
  Customer c = new Customer();
  c.setName("Fred");
  c.setEmail("[email protected]");
  c.setId(12345);
  return c;
}

..and clients like this;

Client client = Client.create();
WebResource resource = client.resource("myHost/getCustomer");
Customer fred = resource.get(Customer.class):

Its beautifully simple.

Qwerky
The problem with JAXB is more than just performance. In fact, I don't really care about performance so much. One of many issues is that I need to consume many simple services where the XML is a little different depending on what REST call I make. With JAXB I have to make a whole new class hierarchy every time to deal with this. It's very ugly and very inefficient coding-time-wise. Maybe I just need to learn JAXB better... For producing services JAXB also makes life difficult when what I want to expose changes depending on who and how I'm being accessed.
at
If you have a lot of different variations in the XML between services, then your going to have to do the work either way, whether you use JAXB annotated classes, or whether you manually parse the XML.
Qwerky
A: 

StAX hasn't been updated in a long time because it hasn't needed to be. The XML format hasn't changed, and StAX is a fairly simple mapping of that format into an API. As low-level XML APIs go, it's pretty easy to use, and i believe quite efficient; however, it is very much a low-level API. It's (very roughly) the XML equivalent of DataInputStream and DataOutputSteam for writing binary data; code written using it will have to be written at the conceptual level of elements, attributes, and so on. I haven't used VTD-XML, but it looks about the same.

If you want to use something higher-level, then have a look at some of the XML serialisation tools, like XStream - could you write some classes which, with the appropriate customisation of the conversion - could be read from your incoming data? Could you then use the same classes for subtly different inputs, with missing fields simply being ignored? In fact, could you use the 'missing XML elements are ignored' approach with JAXB? I don't know how strict it is about schemas, or whether your inputs can be approached in that way. I'm very much shooting in the dark here.

Tom Anderson
A: 

I can recommend Spring MVC 3+. There is out-of-box support for REST services. It takes couple of minutes to create REST service If you know Spring...

http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/

Vitek
This is what I ended up doing.
at