Validating xml against schema in java small cock dating
JAXB2 provides API to validate the generated XML against an XSD in order to verify the correctness of generated XML.
There are choices available to perform Schema Validation Below is the XSD file ["edu.xsd"] used to validate the XML generated via Student & University mapping example.
This post discusses about validating the generated XML against specific XML Schema Definition [XSD].
An XML Schema Definition describes the structure of an XML document and governs the rules and constraints being applied on XML content.
The validate XMLSchema method takes XSD and XML String as argument and return “document is valid” if validation is successful or else returns “document is not valid”.
Please note that Section element in below XSD contains a min Length constraint(4 chars).
We will verify the validation against this constraint. SAXParse Exception; public class Custom Validation Error Handler implements Error Handler Exception in thread "main" bind. SAXParse Exception: cvc-min Length-valid: Value 'Com' with length = '3' is not facet-valid with respect to min Length '4' for type '#Anon Type_section Student'.] at xml.v2.runtime. University; public class Jaxb Schema Validation Exception in thread "main" bind. SAXParse Exception: cvc-min Length-valid: Value 'Com' with length = '3' is not facet-valid with respect to min Length '4' for type '#Anon Type_section Student'.] at xml.v2.runtime.
The validation is successful when the package com.xsd; import sax.
There are numerous tools available for validating an XML document against an XSD.