I am working with Jaxb 2.x and was trying to validate XML document with the given XSD using the following tutorial
hers is the code i have written
unmarshaller.setSchema(schema);
SAXSource source = new SAXSource(new InputSource(xmlFileLocation));
Validator validator = schema.newValidator();
validator.setErrorHandler(new XMLErrorHandler<Object>());
try {
validator.validate(source);
} catch (SAXException e) {
and my XMLErrorHanlder class have following signature
public class XMLErrorHandler<T> implements ErrorHandler {
public void error(SAXParseException exception) throws SAXException {
xmlUnmarshaller.setValidationFlag(true);
log.error(
"Line:Col[" + exception.getLineNumber()
+ ":" + exception.getColumnNumber()
+ "]:" + exception.getMessage());
exception.printStackTrace();
}
}
}
code for warning and fatal has been removed
now its validating the XML with XSD but it only showing the first encountered error while i want to get print on colsole all errors and warning on console
i am not sure where i am doing wrong any help in this will be helpful
Edit1
here is the portion of XSD file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="destination" type="Destination"/>
<xs:complexType name="Destination">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="destinationID" type="xs:string" minOccurs="0"/>
<xs:element name="shortDescription" type="xs:string" minOccurs="0"/>
<xs:element name="longDescription" type="xs:string" minOccurs="0"/>
<xs:element name="stateID" type="xs:string"/>
<xs:element name="typeCode" type="xs:int"/>
<xs:element name="countryCode" type="xs:string"/>
<xs:element name="categories" type="xs:string"/>
<xs:element name="transport" type="Transport" minOccurs="0" maxOccurs="1"/>
<xs:element name="cultures" type="Cultures" minOccurs="0"/>
<xs:element name="events" type="Events" minOccurs="0" maxOccurs="1"/>
<xs:element name="placesToVisit" type="PlacesToVisit" minOccurs="0" maxOccurs="1"/>
<xs:element name="contacts" type="Contact" minOccurs="0" maxOccurs="1"/>
<xs:element name="addresses" type="address" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
and the XML file is
<destination xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="destination.xsd">
<name>Palampur</name>
<destinationID>PLP</destinationID>
<shortDescription>shortDescription</shortDescription>
<longDescription>longDescription</longDescription>
<typeCode>0</typeCode>
<categories>categories</categories>
what my assumption after doing some R&D is that there is some issue with XSD structure or the generated XML but i am not sure abt it
Best Answer
There are a couple approaches you can leverage to validate your XML document against an XML schema.
javax.xml.validation APIs
The first is to use the javax.xml.validation APIs to validate your document against an XML schema without JAXB.
and the XML document:
with the following demo code:
Will give the following output displaying multiple errors:
JAXB APIs
The second approach is to validate while performing an unmarshal operation with JAXB.
For More Information: