Firstly, javax.xml.bind.Validator deprecated in favor of javax.xml.validation.Schema ( javadoc ). The idea is that you parse your schema using javax.xml.validation.SchemaFactory ( javadoc ) and enter this into the marshaller / unmarshaller.
As for your question regarding validation without sorting, the problem here is that JAXB actually delegates the check to Xerces (or whatever SAX processor you use), and Xerces checks your document as a SAX event stream. Therefore, to check, you need to do some sort of sorting.
The least efficient implementation of this will be to use the "/ dev / null" implementation of the SAX processor. Marshalling on a null OutputStream will still involve creating XML, which is wasteful. Therefore, I would suggest:
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(locationOfMySchema); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setSchema(schema); marshaller.marshal(objectToMarshal, new DefaultHandler());
DefaultHandler will discard all events, and the marshal() operation will throw a JAXBException if the validation check for the schema fails.
skaffman Oct. 14 '09 at 7:36 2009-10-14 07:36
source share