I noticed that too.  A few things seem to work better when the document is 
processed by the SAX parser.  Line and column numbers is one example.  PSVI 
augmentation and access to PSVI is another example.  If I call setSchema method 
on DocumentBuilderFactory before parsing my XML file, every node in the DOM 
will have PSVI information (see code below if my description isn't clear 
enough).  However, if I do not validate during load, but use a Validator 
afterwards, PSVI is not augmented.  Also, even though the Validator implements 
PSVIProvider, all calls on this provider return null.  I see it checking 
internally if it's using a SAX Validator, and if it isn't, it returns null.

// Load schema doc.
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.
schemaFactory.setFeature(
Schema schema = schemaFactory.newSchema(
 // Load XML doc.DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
factory.setAttribute(
factory.setNamespaceAware(
factory.setValidating(
factory.setSchema(schema);
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse(
 
 "http://apache.org/xml/properties/dom/document-class-name";, 
"org.apache.xerces.dom.PSVIDocumentImpl");true);true);"C:/work/XML/AgentConfigurationTemplate.xml");W3C_XML_SCHEMA_NS_URI);"http://apache.org/xml/features/validation/schema/augment-psvi";,
 true);newStreamSource(newFile("C:/work/XML/AgentConfiguration.xsd")));


----- Original Message ----
From: Mukul Gandhi <[EMAIL PROTECTED]>
To: j-users@xerces.apache.org
Cc: Alex Talis <[EMAIL PROTECTED]>
Sent: Tuesday, October 28, 2008 6:42:12 AM
Subject: Re: How to get exact location of schema validation errors?

Just an important observation I discovered ...

When I use DOMSource as the XML document source, the error handler
methods fatalError, error and warning get the value -1 for,
getLineNumber() and getColumnNumber() for the SAXParseException
object. i.e., for DOMSource mechanism, retreiving line and column
numbers for the errors/warnings is not possible.

But I get the correct line numbers for errors/warnings if I use,
StreamSource as the XML document source.

So my below suggestion is wrong, for DOMSource. Sorry, it was not tested ...

On Mon, Oct 27, 2008 at 9:06 AM, Mukul Gandhi <[EMAIL PROTECTED]> wrote:
> On Mon, Oct 27, 2008 at 7:07 AM, Alex Talis <[EMAIL PROTECTED]> wrote:
>> I'm using JAXP to validate a DOM Document with a Validator.  When validation
>> fails, I get an exception with a pretty descriptive message, but no way to
>> pinpoint where in the document the error was found.
>
> You can set an error handler to a validator.
>
> for e.g.,
>
>  Validator validator = schema.newValidator();
>  MyErrorHandler errorHandler = new MyErrorHandler();
>  validator.setErrorHandler(errorHandler);
>  validator.validate(new DOMSource ...
>
> Then in MyErrorHandler, you should write as,
>
> class MyErrorHandler implements ErrorHandler {
>
>  public void fatalError(SAXParseException e) throws SAXException {
>
>  }
>
>  public void error(SAXParseException e) throws SAXException {
>
>  }
>
>  public void warning(SAXParseException e) throws SAXException {
>
>  }
>
> within the methods, fatalError, error or warning, you can get location
> of the error/problem as,
>
> e.getLineNumber()
> e.getColumnNumber()


-- 
Regards,
Mukul Gandhi

Reply via email to