Custom Error Handler while Validating XML against XSD Schema

xiaoxiao2021-02-28  17

In this example we create a Custom Error Handler while Validating XML against XSD. We use the javax.xml.validation.Validator to check the XML document against the XSD schema. We can create a custom error handler which will handle the error generated from the validator.validate() method. Next we need to register our custom error handler using the validator.setErrorHandler() method. This will instruct the validator to use our custom error handler when there is an exception.

XSD Schema

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="course"> <xs:complexType> <xs:all> <xs:element type="xs:string" name="name"/> <xs:element type="xs:string" name="description"/> </xs:all> <xs:attribute type="xs:integer" name="id"/> </xs:complexType> </xs:element> </xs:schema>

Custom Error Handler while Validating XML against XSD

We can handle warnings, errrors or fatalErrors. When one of these events occure we can construct a custom error message. In this example the error message contains the line and column number and a message.

package com.sheting.basic.xml.jaxb; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /** * custom error handler while validating xml against xsd */ public class XsdErrorHandler implements ErrorHandler { @Override public void warning(SAXParseException exception) throws SAXException { handleMessage("Warning", exception); } @Override public void error(SAXParseException exception) throws SAXException { handleMessage("Error", exception); } @Override public void fatalError(SAXParseException exception) throws SAXException { handleMessage("Fatal", exception); } private String handleMessage(String level, SAXParseException exception) throws SAXException { int lineNumber = exception.getLineNumber(); int columnNumber = exception.getColumnNumber(); String message = exception.getMessage(); throw new SAXException( "[" + level + "] line nr: " + lineNumber + " column nr: " + columnNumber + " message: " + message); } }

XSD Validator

To validate XML against XSD schema we need to create a schema. And create a validator. We also need to register our custom error handler. We do this by calling the validator.setErrorHandler() method with our custom error handler. When there is a validation exception the validator throws a SAXException. This exception will print our custom error handler exception.

package com.sheting.basic.xml.jaxb; import java.io.IOException; import java.io.StringReader; import javax.xml.XMLConstants; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.xml.sax.SAXException; public class ValidateXmlXsd2 { public static String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<course id=\"1-ID\">\n" + " <name>JAX-B</name>\n" + " <description>Validate XML against XSD Schema</description>\n" + "</course>"; public static void main(String... args) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(ValidateXmlXsd.class.getResource("/schema.xsd")); Validator validator = schema.newValidator(); // Custom Error Handler validator.setErrorHandler(new XsdErrorHandler()); validator.validate(new StreamSource(new StringReader(xml))); System.out.println("Validation is successful"); } catch (IOException e) { // handle exception while reading source } catch (SAXException e) { System.out.println("Message: " + e.getMessage()); } } }
转载请注明原文地址: https://www.6miu.com/read-2603292.html

最新回复(0)