Brett,

Sorry for the delay, some of us are on vacation here...

1. The Xerces SAX parser can do both DTD validation and Schema validation,
although right now the Schema validation is not in sync with the latest W3C 
Working Draft.

2. The following code fragment does what you want.

try {
   Parser parser = 
ParserFactory.makeParser("org.apache.xerces.parsers.SAXParser");   
   ((Configurable)parser).setFeature("http://xml.org/sax/features/validation";, 
true);
   // you need to supply an error handler in order to see an error message!
   parser.setErrorHandler(h);
   parser.parse(args[0]);
} catch (Exception e) {
   e.printStackTrace();
}

----- Original Message ----- 
From: Brett McLaughlin <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, December 30, 1999 10:15 AM
Subject: SAX2 and setting Validation


> I'm posting this again, because nobody answered this the first time-
> 
> I am writing an XML book for O'Reilly, and trying to use Xerces as _the_
> suggested parser, but am going to need to get some answers on this and
> other questions, so please speak up, this could be a nice boost for
> Xerces...
> 
> Two questions:
> 
> 1) Is the SAX Parser in Xerces capable of validation?  I assume it is...
> 
> 2) What is the preferred way to set this mechanism?
> 
>         I know SAX2 and the Configurable interface say to do something
> like:
> 
>         // Request core feature - validation
>         parser.setFeature("http://xml.org/....";, true);
> 
>         parser.parse(uri);
> 
> But to do this, the only way I seem to be able to get the parser is to
> instantiate it directly.  In other words:
> 
>         Parser parser =
> Class.forName("org.apache.xerces.parsers.SAXParser").newInstance();
>         // Do other things with this parser
> 
> But this seems to not be so great.  I would much rather use the
> ParserFactory from SAX:
> 
>         Parser parser =
> ParserFactory.makeParser("org.apache.xerces.parsers.SAXParser");
> 
> I know the differnce isn't much, but it seems to be the best way to do
> this, as well as much more OO (IMHO).  But it doesn't seem that
> org.xml.sax.Parser implements Configurable.  So is there a standard way
> to get a SAX Parser and set it's validation feature, without resorting
> to a "brute-force" newInstance() call on the class?  Any help would be
> appreciated.
> 
> Thanks,
> Brett
> 

Reply via email to