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 Richard Rowell <ri...@bowmansystems.com> on 2003/04/14 21:25:54 UTC

Re: How do I get an XMLReader to stop parsing when the XSD validation fails

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 <ri...@bowmansystems.com>


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