If there's a problem with the data, it's better to find out earlier than later.
In the context of Extensible Markup Language (XML), validation normally involves writing a detailed specification for the document's contents in any of several schema languages such as the World Wide Web Consortium (W3C) XML Schema Language (XSD), RELAX NG, Document Type Definitions (DTDs), and Schematron.
(This description is painted with broad strokes -- there are exceptions.)Until recently, the exact Application Programming Interface (API) by which programs requested validation varied with the schema language and parser.
Since JAXB 1.0 had standard interfaces backed by vendor specific implementation classes it was easy for vendors to code generate the necessary validation logic. Xml Root Element; @Xml Root Element public class Customer In this example we will create an instance of Customer that would produce an XML document that does not conform to our XML schema. XMLError Reporter.report Error(XMLError Reporter.java:384) at org.apache.xerces. XMLError Reporter.report Error(XMLError Reporter.java:318) at org.apache.xerces.xs. XMLSchema Validator$XSIError Reporter.report Error(XMLSchema Validator.java:417) at org.apache.xerces.xs.
RELAX NG only works if you manage to configure the JAXP validation framework properly and add additional third party libraries.
The schemas (or DTDs) needed for validation can be specified as can provide the schema to the parser.
When JAXB 2.0 moved to annotated POJO classes the ability to have generated validation logic was lost and Validator was deprecated. The customers name will be too long, and it will contain more than two phone numbers.package blog.validation; import XMLSchema Validator.report Schema Error(XMLSchema Validator.java:3181) at org.apache.xerces.xs.
In this post I'll demonstrate how to leverage the validation APIs to get the same sort of behaviour.package blog.validation; import XMLSchema Validator.element Locally Valid Type(XMLSchema Validator.java:3096) at org.apache.xerces.xs.
XMLSchema Validator.handle End Element(XMLSchema Validator.java:2149) at org.apache.xerces.xs.