Skip to main content

Schema Validation not returning Error Element Line Number and Coumn Number

2 replies [Last post]
cpuburst
Offline
Joined: 2008-02-13

Hi

I am using JAXP 1.3 Instance Schema Validation feature.
The following is the code I use to do the former.

-----------------------------------------------------------------------------------------------------------------

DOMSource xmlsource = new DOMSource(myInstanceDocument);
// create a SchemaFactory for loading W3C XML Schemas
SchemaFactory wxsfactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//set the errorhandler for handling errors in schema itself
wxsfactory.setErrorHandler(getNewErrorHandlerInstance(myErrorHandlerClassName));
//load a W3C XML Schema
schema = wxsfactory.newSchema(new StreamSource(mySchemaInputstream));

/** Code to check if Schema itself had errors goes here **/

// create a validator from the loaded schema
Validator validator = schema.newValidator();
NdOfflineSchemaErrorHandler handler = getNewErrorHandlerInstance(myErrorHandlerClassName);
// set the errorhandler for handling validation errors
validator.setErrorHandler(handler);
// validate the XML instance
validator.validate(xmlsource);
return handler;

---------------------------------------------------------------------------------------------------------------

Now the issue is that my error handler traps and saves all Fatal Errors, Errors and Warnings in holder objects during the error event call backs, and gets exception details from the SAXParseException. I am unable to get correct line numbers or column numbers from SAXParseException. It is returning -1 always.

When I inspect the Xerces 2.7.1 Source Code, I see there is an XMLLocator object that is set using SimpleLocator. Is there a way to specify an external XMLLocator? Any other ideas?

The following is the code snippet of xerces2.7.1, validate method. Please see the code in bold font.

public void validate(Source source, Result result)
throws SAXException, IOException {
if (result instanceof DOMResult || result == null) {
final DOMSource domSource = (DOMSource) source;
final DOMResult domResult = (DOMResult) result;
Node node = domSource.getNode();
fRoot = node;
if (node != null) {
fComponentManager.reset();
fValidationManager.setEntityState(this);
fDOMNamespaceContext.reset();
String systemId = domSource.getSystemId();
fXMLLocator.setLiteralSystemId(systemId);
fXMLLocator.setExpandedSystemId(systemId);
fErrorReporter.setDocumentLocator(fXMLLocator);

I am using Xerces 2.7.1 , JDK 1.6

Any help would be greatly appreciated.

Warm Regards
Sanoj Antony

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cpuburst
Offline
Joined: 2008-02-13

I am able to get line numbers and column numbers after I serialized the Instance document and used SAXSource instead of DOMSource.

This is not exactly what I was looking for as I guess the serialization (Identity transform in this case ) introduces some performance penalty.
Any way to make it work with a DOM Document will be greatly appreciated.

regards
Sanoj

cpuburst
Offline
Joined: 2008-02-13

Posting a Unit test client for carrying out schema validation plus an xml and it's schema document
--------------------------------------------------------------------------------------

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
*
*
*
*/
public class TestSchemaValidation
{

/**
* @param args
*/
public static void main(String[] args)
{
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
String xmlPath = "c:\\UnitTesting\\SchemaValidation\\Address.xml";
Document doc=null;
try {
doc = fac.newDocumentBuilder().parse(new FileInputStream(new File(xmlPath)));
ErrorHandler handler = new TestSchemaValidation().doInstanceSchemaValidation(doc);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (SAXException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ParserConfigurationException e) {
e.printStackTrace();
}
}

/**
*
* @param dataDocv
*/
public MyErrorHandler doInstanceSchemaValidation(Document dataDoc)
{
Schema schema;
MyErrorHandler schemaErrorHandler = new MyErrorHandler();
MyErrorHandler instDocErrorHandler = new MyErrorHandler();
DOMSource xmlsource;
try {
String schemaPath = "c:\\UnitTesting\\SchemaValidation\\AddressSchema.xsd";
FileInputStream schemaIns = new FileInputStream(new File(schemaPath));
xmlsource = new DOMSource(dataDoc.getDocumentElement());
// create a SchemaFactory for loading W3C XML Schemas
SchemaFactory wxsfactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//set the errorhandler for handling errors in schema itself
wxsfactory.setErrorHandler(schemaErrorHandler);
//load a W3C XML Schema
schema = wxsfactory.newSchema(new StreamSource(schemaIns));
// create a validator from the loaded schema
Validator validator = schema.newValidator();
// set the errorhandler for handling validation errors
validator.setErrorHandler(instDocErrorHandler);
// validate the XML instance
validator.validate(xmlsource);
return instDocErrorHandler;
}
catch (SAXException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return instDocErrorHandler;
}

class MyErrorHandler implements ErrorHandler {

/**
*
*/
public void error(SAXParseException exception) throws SAXException
{
System.out.println("Error - " + exception.getMessage());
System.out.println("Line Number = " + exception.getLineNumber());
System.out.println("Column Number = " + exception.getColumnNumber());
}
/**
*
*/
public void fatalError(SAXParseException exception) throws SAXException
{
System.out.println("Fatal Error - " + exception.getMessage());
System.out.println("Line Number = " + exception.getLineNumber());
System.out.println("Column Number = " + exception.getColumnNumber());
}

public void warning(SAXParseException exception) throws SAXException
{
System.out.println("Warning - " + exception.getMessage());
System.out.println("Line Number = " + exception.getLineNumber());
System.out.println("Column Number = " + exception.getColumnNumber());
}
}
}

XML Document
--------------------------


345
Sunshine
Bangalore
India
98

XML Schema
-------------------------



























Regards
Sanoj