Get Line Number from XML Validation against XSD Schema

In this example we will grab the Line and column number from the validation exception while validate XML against XSD Schema. We use the javax.xml.validation.Validator to check the XML document against the XSD schema. Be carefull because the validator is not thread safe. It is the responsibility of the application to make the code thread safe. When there is a validation exception the validator throws a SAXException. The validator throws also a SAXParseException. The great thing about the SAXParseException is that this has methods to retrieve the line and column number from the exception.

Project structure

+--src
|   +--main
|       +--java
|           +--com
|               +--memorynotfound
|                   |--ValidateXmlXsd.java
|       +--resources
|           |--schema.xsd
pom.xml

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>

Getting the Line Number from XML Validation against XSD Schema

To validate XML against XSD schema we need to create a schema. And create a validator. When there is a validation exception the validator throws a SAXException. The validator throws also a SAXParseException. The great thing about the SAXParseException is that this has methods to retrieve the line and column number from the exception.

package com.memorynotfound.xml.xsd;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
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 java.io.IOException;
import java.io.StringReader;

public class ValidateXmlXsd {

    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 {
            System.out.println("Validate XML against XSD Schema");
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(ValidateXmlXsd.class.getResource("/schema.xsd"));
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new StringReader(xml)));
            System.out.println("Validation is successful");
        } catch (IOException e) {
            // handle io exception on validate
        } catch (SAXParseException e) {
            int line = e.getLineNumber();
            int col = e.getColumnNumber();
            String message = e.getMessage();
            System.out.println("Error when validate XML against XSD Schema\n" +
                    "line: " + line + "\n" +
                    "column: " + col + "\n" +
                    "message: " + message.substring(message.indexOf(":") + 2));
        } catch (SAXException e) {
            // handle general sax exception
        }
    }
}

Output

Validate XML against XSD Schema
Error when validate XML against XSD Schema
line: 2
column: 19
message: '1-ID' is not a valid value for 'integer'.

References

Download

You may also like...