Best Java XML parser for producing/consuming REST services
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 up开发者_运维技巧dated in about 4 years. Is VTD-XML the state of the art in XML processing in Java?
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>fred@email.com</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("fred@email.com");
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.
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.
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/
精彩评论