XML Schema Validation using JAXB

Hi All,

This is a very small post on a very important part of XML Schema validation. While interchanging the data across different systems using XML format, it becomes  more important to validate the XML data with XML schema. As XSD plays a crucial part to validate all the possible combination of data to carry in XML document.

Now lets see an example of performing XSD Validation using JAXB.

//Setting the Validation
Schema schema;
SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI ); //W3C XML Schema Namespace URI.
schema = schemaFactory.newSchema(new File(“src/product.xsd”)); //THE XSD Location
marshaller.setSchema(schema); //Register the schema in marshaller
marshaller.marshal(product, System.out); //marshal the object

To list all the validation  issues in logger  or on system console, you can use my previous post on registering ValidationHandler in JAXB.(Read POST http://wp.me/pQKn2-gj )

Hope this helps.


Thanks
R Vashi

Advertisements

One thought on “XML Schema Validation using JAXB

  1. bhakti

    hey i can an error while trying to validate. the code is similar to what u have given above.
    The errror is:
    Exception in thread “main” javax.xml.bind.MarshalException
    – with linked exception:
    [org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘xsi:BSM’.]
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:326)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
    at SerializeModel.main(SerializeModel.java:96)
    Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘xsi:BSM’.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(Unknown Source)
    at org.xml.sax.helpers.XMLFilterImpl.startElement(Unknown Source)
    at com.sun.xml.bind.v2.runtime.output.SAXOutput.endStartTag(SAXOutput.java:128)
    at com.sun.xml.bind.v2.runtime.output.ForkXmlOutput.endStartTag(ForkXmlOutput.java:106)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.endAttributes(XMLSerializer.java:307)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:592)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:342)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323)
    … 3 more

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s