Java - XSD Validator : Get the node of the element containing the validation error

986 Views Asked by At

Hie, I am working in Java 8 and currently I am trying to validate XML with XSD schema using a validator (javax.xml.validation.Validator). My goal is to be able to retrieve the node of the element containing the validation error.

In my code I used an ErrorHandler that I applied to my Validator. In addition, I added a getCurrentNode() method which is supposed to return the error node (validator.getProperty("http://apache.org/xml/properties/dom/current-element-node"). In my case the getProperty("---") method returns null instead of a Node Object. I don't understand why? I hope it's not because of a version problem of one of my components...Can someone more knowledgeable than me understand what is going wrong?

I have taken the code from the following response: Get parent element on XSD validation error .

public static void validateXMLSchema(URL xsd, String xml) throws SAXException, IOException {

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(xsd);

    Validator validator = schema.newValidator();

    validator.setErrorHandler(new MyErrorHandler(validator));


    StreamSource ssXmlPath = new StreamSource(xml); //xml is a String represanting the path file
    validator.validate(ssXmlPath);

}

private static class MyErrorHandler implements ErrorHandler {
    private final Validator xsdValidator;

    public MyErrorHandler(Validator xsdValidator) {
        this.xsdValidator = xsdValidator;
    }
    @Override
    public void warning(SAXParseException exception) throws SAXException {
        System.out.println("Warning on node: " + getCurrentNode());
        System.out.println(exception.getLocalizedMessage());

    }

    @Override
    public void error(SAXParseException exception) throws SAXException {
        System.out.println("Error on node: " + getCurrentNode());
        System.out.println(exception.getLocalizedMessage());
    }

    @Override
    public void fatalError(SAXParseException exception) throws SAXException {
        System.out.println("Fatal on node: " + getCurrentNode());
        System.out.println(exception.getLocalizedMessage());
    }


    private Node getCurrentNode() throws SAXNotRecognizedException, SAXNotSupportedException {
        // get prop "http://apache.org/xml/properties/dom/current-element-nodeb"
        // see https://xerces.apache.org/xerces2-j/properties.html#dom.current-element-node
        Node node = (Node)xsdValidator.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY);
        System.out.println(node.getLocalName() + ": " + node.getTextContent());
        return node;
    }
}
1

There are 1 best solutions below

0
On

Solved : To be able to use nodes you have to use Document Object, if you validate a file directly without Document there is no DOM construction and Xerces can't find any node.

I have to had something like :

DocumentBuilderFactory dbf
DocumentBuilderFactory.newInstance(org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.class.getName(), XSDTest.class.getClassLoader());
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(xmlData));