Re: Validating xml file with schema using xerces-j

Just to add some hints and a question...

Rahul Srivastava wrote:

>>Nguyen, Thang wrote...
>>
>>Hi, 
>>I'm currently trying to figure out how to make my program validate
>>an xml file againt a schema file using xerces-j
>>
> 
> First question is which version of xerces-j are you using?. If you are using 
> xerces2, then, hold it!. Xerces2 does not support schema support, as of now.
> 
> If you are using xerces1 (1.4.x), then you can either instantiate parser as 
> SAXParser or DOMParser. Set the feature on to support validation and invoke 
> parser.parse(). This will validate your xml file against schema.


For instance:

  Parser p = new org.apache.xerces.parsers.SAXParser();
  p.setDocumentHandler(this);
  ((XMLReader)p).setFeature( "http://xml.org/sax/features/validation",
		true);
  ((XMLReader)p).setFeature(
      "http://apache.org/xml/features/validation/schema",
       true );
  ((XMLReader)p).setFeature(
      "http://apache.org/xml/features/validation/schema-full-checking",
       true );
  p.parse(uri);


> 
>>I can't seem to find which class to instantiate to include the schema file
>>(xsd)
>>
> 
> This question is not very clear to me.
> The xml instance file points to the schema file against which validation is to 
> be performed. So, the schema file is picked from the location, pointed out by 
> xml instance and carries on with the validation process.


Not necessarly: you can define the location of a schema in the instance 
document but also through a set of other SAX properties 
(http://apache.org/xml/properties/schema/external-schemaLocation
  and 
http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation
).

You will find their definition at 
http://xml.apache.org/xerces-j/properties.html

Hope this helps

Eric


> Why do you need to instantiate a class to include schema.
> 
> -Rahul.
> 
> 
>>thanks for any help,
>>
>>
>>
> 
> 
> 



-- 
Rendez-vous à Paris pour une visite guidee de la nebuleuse XML.
                                           http://dyomedea.com/formation/
------------------------------------------------------------------------
Eric van der Vlist       http://xmlfr.org            http://dyomedea.com
http://xsltunit.org      http://4xt.org           http://examplotron.org
------------------------------------------------------------------------

Received on Thursday, 11 October 2001 02:37:34 UTC