How do I serialize / deserialize a class in XML with Woodstox StAX 2
I'm pretty much trying to archive, what has been done in how-to-serialize-deserialize-simple-classes-to-xml-and-back (C#) in Java. If possible, I would like to avoid writing a serialize / deserialize methods for each class.
For example, part of serialize:
XMLOutputFactory xof = null;
XMLStreamWriter2 writer = null;
try {
resp.开发者_如何转开发setContentType("text/plain");
xof = XMLOutputFactory.newInstance();
writer = (XMLStreamWriter2) //
xof.createXMLStreamWriter(resp.getOutputStream());
writer.writeStartDocument("1.0");
writer.writeStartElement("data");
//
// Magic happens here.
//
writer.writeEndElement();
writer.writeEndDocument();
} catch (XMLStreamException e) {
e.printStackTrace();
resp.sendError(1, "Problem 1 occured.");
} finally {
try {
writer.flush();
writer.close();
} catch (XMLStreamException e) {
e.printStackTrace();
resp.sendError(2, "Problem 2 occured.");
}
}
Not part of this question, as I'm trying to tackle problems 1 by 1, but might give you a sense of what I'm trying to do. When I deserialize, I would also like to check if the input is valid. Eventually I want to use XSLT transforms with serialized form.
JAXB is how you serialize Java objects to XML. The following will help you get started:
- http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted
JAXB Implementations
There are several implementations of this standard:
- EclipseLink MOXy (I'm the tech lead)
- Metro (the reference implementation, included in Java SE 6)
- JaxMe
Woodstox StAX 2
JAXB accepts many input/output formats including StAX.
Validation
XML is converted to objects using an Unmarshaller, and objects are converted to XML with a Marshaller. You can set an instance of javax.xml.validation.Schema to validate the input during these operations.
You can also use the javax.xml.validation APIs directly with JAXB, check out the following for an example:
- Checking a java value with an xml schema
XSLT
The javax.xml.transform libraries are used in Java to perform XSLT transforms. JAXB is designed to work with these libraries using JAXBSource and JAXBResult.
For More Information
Check out my blog:
- http://bdoughan.blogspot.com
In addition to the comprehensive accepted answer, it's worth noting that Woodstox (or any Stax2 implementation) can actually validate both input and output; see this blog entry for sample code. One benefit is that you can also validate against Relax NG (not supported AFAIK by JAXP parser that JAXB uses by default) or DTD.
Also: there is a new project called Jackson-xml-databinder (a spin-off of Jackson JSON processor) that implements "mini-JAXB" (subset of full JAXB functionality) using a Stax2 parser (like Woodstox or Aalto). Main benefits are bit more powerful data binding part and even better performance than JAXB implementations; downside that it is not as mature, and does not support all XML specific aspects. It is probably most useful in cases where both JSON and XML formats are to be supported.
精彩评论