You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Alex Talis <al...@yahoo.com> on 2008/10/27 02:37:22 UTC

How to get exact location of schema validation errors?

Hi,

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.  Is there a way to find out exactly where the error was found, down to the attribute level, e.g. as an XPath expression, or as a unique ID that is part of the node?  From some other posts on this list it doesn't look good, but logically, the validator must know the location...  Maybe there's a way to plug into the XNI layer and pass this information upstream?

Thank You,

Alex

Re: How to get exact location of schema validation errors?

Posted by Mukul Gandhi <ga...@gmail.com>.
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 <ga...@gmail.com> wrote:
> On Mon, Oct 27, 2008 at 7:07 AM, Alex Talis <al...@yahoo.com> 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

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: How to get exact location of schema validation errors?

Posted by Mukul Gandhi <ga...@gmail.com>.
On Mon, Oct 27, 2008 at 7:07 AM, Alex Talis <al...@yahoo.com> 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

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org