To add to that, if it was a user asking I'd recommend SchemaFactory of those three choices since it's in Java 5+ and would work in environments where Xerces isn't available.
Michael Glavassevich XML Parser Development IBM Toronto Lab E-mail: mrgla...@ca.ibm.com E-mail: mrgla...@apache.org mrgla...@ca.ibm.com wrote on 06/12/2009 07:52:11 AM: > Hi Mukul, > > All of those approaches end up going through similar codepaths (i.e. > XMLSchemaLoader) and are appropriate for a validating a schema, > though those APIs do exist primarily for other purposes: > > SchemaFactory - entry point into the JAXP Validation API for loading > schemas for validation > XSLoader - entry point into the XML Schema API for obtaining an > XSModel for analysis/processing of the component model > XMLGrammarPreparser - an API for preparsing schemas and DTDs for use > in grammar caching (i.e. a lower-level alternative to SchemaFactory) > > Thanks. > > Michael Glavassevich > XML Parser Development > IBM Toronto Lab > E-mail: mrgla...@ca.ibm.com > E-mail: mrgla...@apache.org > > Mukul Gandhi <gandhi.mu...@gmail.com> wrote on 06/12/2009 06:14:47 AM: > > > Hi Hiranya, > > Interestingly, I have found an easy way to validate a Schema > > document. We just need to use the JAXP validation API (which will be > > visible to the user normally). > > > > Below is a code snippet for this: > > > > SchemaFactory sf = SchemaFactory.newInstance .. > > sf.setErrorHandler .. > > Schema s = sf.newSchema(new StreamSource(schemapath)); > > > > The 'newSchema' call would not succeed if XML Schema has an error. > > > > The XSModel or the Grammar technique you wrote, is a low level API > > which is normally used within Xerces. > > > > On Fri, Jun 12, 2009 at 11:49 AM, Mukul Gandhi<gandhi.mu...@gmail. > com> wrote: > > >> By going through the Xerces samples I came up with the following code to > > >> validate an XML schema document. > > >> > > >> XMLGrammarPreparser preparser = new XMLGrammarPreparser(); > > >> preparser.registerPreparser(XMLGrammarDescription. > > XML_SCHEMA, null); > > >> preparser.setFeature ("http://xml.org/sax/features/namespaces", > > >> true); > > >> preparser.setFeature ("http://xml.org/sax/features/validation", > > >> true); > > >> > > >> preparser.setFeature ("http://apache.org/xml/features/validation/schema", > > >> true); > > >> preparser.setErrorHandler(new MyErrorHandler()); > > >> Grammar g = > > >> preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, > > >> new XMLInputSource(null, xsdUrl, null)); > > >> > > >> This seems to be working really well. It even validates any > > imported schemas > > >> etc. All the errors are reported to the registered error > handler.WDYT about > > >> this approach over the XSModel construction approach? > > > > > > I think, you can use either of the approaches. The XSModel approach I > > > mentioned also validates imported schemas as well. > > > > > > Infact, the 'loadURI' method I mentioned, builds XSModel from the > > > Grammar object (it does something like, ((XSGrammar) > > > fSchemaLoader.loadGrammar(new XMLInputSource(null, uri, > > > null))).toXSModel(). > > > > > > -- > > Regards, > > Mukul Gandhi > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: j-dev-unsubscr...@xerces.apache.org > > For additional commands, e-mail: j-dev-h...@xerces.apache.org