Skip to main content

Validation error does not include line or column number

1 reply [Last post]
djgover
Offline
Joined: 2007-05-03
Points: 0

Hello,

I am using jaxp validation to check an xml document against a schema, here is my basic code:

Handler handler = new Handler();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setErrorHandler(handler);
Schema newSchema = schemaFactory.newSchema(mySchemaURL);
Validator schemaValidator = newSchema.newValidator();
schemaValidator.setErrorHandler(handler);
schemaValidator.validate(payload);

if there is a schema validation error the error handler is called, here is sample code:

public void error(SAXParseException sAXParseException) throws SAXException
{
sAXParseException.getColumnNumber;
sAXParseException.getLineNumber;
}

when I look at the column and line values within the SAXParseException object it always contains "-1". I am using the newest jaxp jars v142.

Does anyone know how to enable these values ?

thanks!
DG

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hille
Offline
Joined: 2007-06-24
Points: 0

"-1" is the exspected return-value from a SAXParseException.getLineNumber resp. getColumnNumber if no line- / column-information is available. Otoh I don't know when it is appropriate for a jaxp-implementation to "say so". As you do neither post a self-contained piece of your validation code nor provide any test-data ("xsd" + "xml") it's unnecessarly hard to dig any further...

You may run your data against the following code (java 1.6.0_01 + jaxp 1.4.2) which principally does provide line- / column information in case of errors.


package info.test.stax;

import java.io.File;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
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;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

public class ValidatorExample {

protected static class ErrorHandler extends DefaultHandler {

public void error( SAXParseException parseException ) throws SAXException {
printException( parseException );
}

public void fatalError( SAXParseException parseException ) throws SAXException {
printException( parseException );
}

public void warning( org.xml.sax.SAXParseException parseException )
throws org.xml.sax.SAXException {
printException( parseException );
}

private void printException( SAXParseException exception ) {
System.out.println( exception );
System.out.println( "line/column: " + exception.getLineNumber() + "/" + exception.getColumnNumber() );
}

}

public static void main( String[] args ) throws Exception {
if( args.length != 2 ) {
System.out.println( "Usage : ValidatorExample " );
} else {
XMLInputFactory xmlif = XMLInputFactory.newInstance();
xmlif.setProperty( XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
Boolean.FALSE );

SchemaFactory schemaFactory = SchemaFactory
.newInstance( "http://www.w3.org/2001/XMLSchema" );
Schema schemaGrammar = schemaFactory
.newSchema( new File( args[1] ) );

Validator schemaValidator = schemaGrammar.newValidator();
schemaValidator.setErrorHandler( new ErrorHandler() );

Source source = new StreamSource( args[0] );
XMLStreamReader xmlr = xmlif.createXMLStreamReader( source );

schemaValidator.validate( source );

while( xmlr.hasNext() ) {
int eventType = xmlr.next();
System.out.println( "Event of type: " + eventType );
}
}
}
}

Hille.