Custom Error Handler while Validating XML against XSD Schema

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());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值