I tried the 2nd solution, to throw an exception from the ErrorHandler
methods, and it works.

It seems like what I am trying to do is a pretty basic thing - don't process
the file if it is not valid.  I was too zeroed in on finding a method like
setAbortOnValidationError(true) somewhere within Xerces, that I overlooked
the obvious solution of throwing an exception from ErrorHandler.

Thanks for the help Eric and Richard.


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] 
Sent: Monday, April 14, 2003 2:43 PM
To: [EMAIL PROTECTED]
Subject: Re: How do I get an XMLReader to stop parsing when the XSD
validation fails



Err...  System.exit() terminates the whole JVM, so not "elegant enough for
your tastes" is quite the understatement. ;^)

I think the usual (intended?) thing is do as Richard's second
suggestion--more specifically throw a SAXException from the ErrorHandler's
error(), warning(), and fatalError() methods; XmlReader.parse() will pass on
/ re-throw that same SAXException.





 

                      Richard Rowell

                      <[EMAIL PROTECTED]        To:
[EMAIL PROTECTED]                                    
                      stems.com>               cc:

                                               Subject:  Re: How do I get an
XMLReader to stop parsing when the XSD      
                      04/14/2003 03:25          validation fails

                      PM

                      Please respond to

                      xerces-j-user

 

 





On Mon, 2003-04-14 at 13:54, Ahearn, Denis wrote:
> Hello,
>
> Is there a way to have the XMLReader.parse()  method stop if the XML 
> file being parsed is invalid with respect to an associated XML schema 
> (XSD)?  I have poured through the online documentation as well as the 
> Xerces JavaDocs, and didn't find anything that would allow me to 
> control this behavior.
>
> I have the following features in my XMLReader set to "on".
>
> http://xml.org/sax/features/validation
> http://apache.org/xml/features/validation/schema
> http://apache.org/xml/features/validation/schema-full-checking
>
> When running with an XML file that has some schema errors in it, I do 
> see the validation errors being logged to my console window, however 
> the XMLReader still parses the XML file.  In this case, I would like 
> to be able to avoid processing the file (i.e. not do anything with the 
> data in the file), and instead inform the user that the XML file 
> contains validation errors and the program can't proceed.

I'm not Xerces expert, so there may be a simpler way, but...

Couldnt you register your own custom error handler, and simply kill the
thread after you log/report the error?  IE:

public class MySAXApp extends DefaultHandler
{
  public MySAXApp (){ super();}
  public void startDocument (){}
  ...//other methods here
  public void error(SAXParseException e)
  {
    System.out.println("** " +e.getMessage() + " **");
    System.out.println("LINE: " + e.getLineNumber());
    System.out.println("PUBLIC_ID: " + e.getPublicId());
    System.out.println("SYSTEM_ID: " + e.getSystemId());
    System.exit();
  }
  publiv static void main()
  {
    XMLReader xr = new SAXParser();
    ...//turn on options here
    MySAXApp handler = new MySAXApp();
    xr.setErrorHandler(handler);
    ...//parse here
   }

If the call to system.exit() isn't elegant enough for your tastes, You may
alternatively be able to throw an exception in the error() function and
catch it outside the call to parse() (I haven't tried this though).

--
Richard Rowell <[EMAIL PROTECTED]>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]







---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to