Castor XML on a Real-World Project
by Tom Marrs
On my current project, I can receive an XML request document that hase multiple possible root elements, so I can't know ahead of time which type of object to use for unmarshalling. But Castor lets meconfigure the Unmarshaller ahead of time with the Java classes I'm expecting. Then, I can just do an unmarshall, check the request type, and process appropriately.
My code runs inside a Servlet that takes an XML Request, unmarshalls it into POJOs, invokes a backend service, and marshalls an XML Response in the HttpResponse. I had some trouble with the Servlet until I remembered to close() the PrintWriter (which wrote the XML text to the HttpResponse) and to set output to text/html and use UTF-8 encoding.
I use HttpUnit to send XML requests over HTTP and check the response (also in XML). HttpUnit works as if you were entering requests using a brower. But HttpUnit fits in nicely with an automated build environment and Continous Integration.
I've only had a couple of problems with Castor. Castor's validation works reasonably well, but it lets me use an invalid date format for an xsd:dateTime. I also can't use Spring XML Bean files to populate Castor objects because the Castor-generated objects don't follow the JavaBean rules for collections.
Despite these minor drawbacks, Castor is easy to use and its code generator saved me a lot of time when I had to change my XSD schema.
What's been your experience with Castor XML?