[prev in list] [next in list] [prev in thread] [next in thread]
List: xerces-j-dev
Subject: Re: XML validation using xml schema string
From: "Praveen Peddi" <ppeddi () contextmedia ! com>
Date: 2003-01-27 13:58:43
[Download RAW message or body]
Thanks Jeff. It worked perfectly....
Praveen
----- Original Message -----
From: "Jeff Greif" <jgreif@alumni.princeton.edu>
To: <xerces-j-user@xml.apache.org>
Sent: Friday, January 24, 2003 3:11 PM
Subject: Re: XML validation using xml schema string
> Try setting the schemalocation of your schema (noNamespaceLocation) to
match
> the fakeLocationURI used for the entity resolver:
>
>
>
parser.setProperty("http://apache.org/xml/properties/schema/external-noNames
> paceSchemaLocation","http://www.contextmedia.com");
> parser.setEntityResolver(new
> SchemaEntityResolver(parser.getEntityResolver(), schemaString,
> "http://www.contextmedia.com"));
>
> Jeff
>
> ----- Original Message -----
> From: Praveen Peddi
> To: xerces-j-user@xml.apache.org
> Sent: Friday, January 24, 2003 11:34 AM
> Subject: Re: XML validation using xml schema string
>
>
> Hi Jeff,
> I did exactly what you told and it doesn't seem to work. Please look at
the
> following code and see if I am doing anything wrong.
> The error I get is:
> **Parsing Error**
> Line: 1
> URI: null
> Message: cvc-elt.1: Cannot find the declaration of element 'content'.
> SAXException Exception
> Total time of SAXValidate:3546
> org.xml.sax.SAXException: Error Validating the content XML.
> at XMLValidator.validateXmlAgainstLocalSchema(XMLValidator.java:105)
>
> My Entity Resolver is:
> public class SchemaEntityResolver implements EntityResolver {
>
> InputSource source;
> EntityResolver defaultResolver;
> String fakeLocationURI;
>
>
> public SchemaEntityResolver(EntityResolver defaultResolver, String
> mySchemaString,
> String fakeLocationURI) {
> source = new InputSource(new StringReader(mySchemaString));
> this.defaultResolver = defaultResolver;
> this.fakeLocationURI = fakeLocationURI;
> }
>
>
> public InputSource resolveEntity(String pubId, String sysId)
> throws IOException, SAXException {
> if ((pubId != null && pubId.equals(fakeLocationURI))
> || (sysId != null && sysId.equals(fakeLocationURI))) {
> return source;
> }
> return defaultResolver.resolveEntity(pubId, sysId);
> }
> }
>
> And My validate method is as follows:
>
> public static void validateXmlAgainstLocalSchema(String XMLString, String
> schemaString) throws SAXException{
> long startTime = System.currentTimeMillis();
> System.out.println("Files: XML:" + XMLString + " XSD:" +
> schemaString);
> XMLReader parser = null;
> // Instantiate a parser
> try{
> parser =
> XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
> } catch (org.xml.sax.SAXException ex){
> System.out.println("SAXException Exception");
> throw new SAXException("Error Obtaining the parser.");
> }
>
> try{
>
> // Register the error handler
> parser.setErrorHandler(new SchemaErrorHandler());
>
>
>
//parser.setFeature("http://apache.org/xml/features/continue-after-fatal-err
> or", true);
> // Turn on validation
>
> parser.setFeature("http://xml.org/sax/features/namespaces",true);
>
parser.setFeature("http://xml.org/sax/features/validation",
> true);
>
>
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
>
>
parser.setFeature("http://apache.org/xml/features/validation/schema-full-che
> cking",true);
>
>
//parser.setProperty("http://apache.org/xml/properties/schema/external-noNam
> espaceSchemaLocation",Schema);
> parser.setEntityResolver(new
> SchemaEntityResolver(parser.getEntityResolver(), schemaString,
> "http://www.contextmedia.com"));
>
>
> } catch (org.xml.sax.SAXNotRecognizedException ex){
> ex.printStackTrace();
> System.out.println("SAXNotRecognizedException
> Exception");
> } catch (org.xml.sax.SAXNotSupportedException ex){
> ex.printStackTrace();
> System.out.println("SAXNotSupportedException
> Exception");
> }
>
> // Parse the document
> try{
> //String XMLSystemId = new
> File(XMLString).toURL().toExternalForm();
> parser.parse(new InputSource(new
StringReader(XMLString)));
> //parser.parse(XMLSystemId);
> System.out.println("Parsed Successfully by SAX Parser");
> } catch (org.xml.sax.SAXException ex){
> System.out.println("SAXException Exception");
> throw new SAXException("Error Validating the content XML.");
> } catch (java.io.IOException ex){
> System.out.println("IOException Exception");
> throw new SAXException("Error Validating the content XML.");
> } finally {
> long endTime = System.currentTimeMillis();
> System.out.println("Total time of
> SAXValidate:"+(endTime-startTime));
> }
> }
>
> ----- Original Message -----
> From: "Jeff Greif" <jgreif@alumni.princeton.edu>
> To: <xerces-j-user@xml.apache.org>
> Sent: Thursday, January 23, 2003 3:07 PM
> Subject: Re: XML validation using xml schema string
>
>
> > Something like this (just typed into mail message, neither compiled nor
> > tested).
> >
> > in main:
> >
> > DomParser parser = new DomParser();
> > parser.setEntityResolver(new
> > MyEntityResolver(parser.getEntityResolver(),
> >
> > mySchemaString, myfakeSchemaLocationURI));
> >
> > class MyEntityResolver {
> > InputSource source;
> > EntityResolver defaultResolver;
> > String fakeLocationURI;
> > public MyEntityResolver(EntityResolver default, String
mySchemaString,
> > String fakeLocationURI) {
> > source = new InputSource(new StringReader(mySchemaString));
> > defaultResolver = default;
> > this.fakeLocationURI = fakeLocationURI;
> > }
> > public InputSource resolveEntity(String pubId, String sysId) {
> > if ((pubId != null && pubId.equals(fakeLocationURI))
> > || (sysId != null && sysId.equals(fakeLocationURI))) {
> > return source;
> > }
> > return defaultResolver.resolveEntity(pubId, sysId);
> > }
> >
> > }
> >
> > Jeff
> > ----- Original Message -----
> > From: "Praveen Peddi" <ppeddi@contextmedia.com>
> > To: <xerces-j-user@xml.apache.org>
> > Sent: Thursday, January 23, 2003 10:51 AM
> > Subject: Re: XML validation using xml schema string
> >
> >
> > > Jeff,
> > > I think I didn't get you completely.
> > >
> > > Could you please write a sample code or modify my code below so that I
> can
> > > understand what you are syaing.
> > >
> > > Thanks
> > > Praveen
> > > ----- Original Message -----
> > > From: "Jeff Greif" <jgreif@alumni.princeton.edu>
> > > To: <xerces-j-user@xml.apache.org>
> > > Sent: Thursday, January 23, 2003 11:28 AM
> > > Subject: Re: XML validation using xml schema string
> > >
> > >
> > > > I'm not able to give information correct in detail, but here is an
> > outline
> > > > of what must happen:
> > > >
> > > > The entity resolver is called with a namespace URI or a schema
> location
> > as
> > > > an argument, I think. In the course of parsing your instance
document
> > > (.xml
> > > > file), it will be called to process your schema's namespace and
> possibly
> > > > other namespaces (e.g. the XMLSchema-instance namespace) mentioned
> > > therein.
> > > > Probably you should get the default entity resolver for the parser
> > (which
> > > > knows how to handle some things, presumably), and create your own
> entity
> > > > resolver holding that one as a member. Then when asked for a
> particular
> > > > schema by its namespace or location, if it's your schema, return the
> > input
> > > > source as your code below does, otherwise, pass the request to the
> > member
> > > > resolver which knows better than you what to do.
> > > >
> > > > Jeff
> > > >
> > > > ----- Original Message -----
> > > > From: "Praveen Peddi" <ppeddi@contextmedia.com>
> > > > To: <xerces-j-user@xml.apache.org>
> > > > Sent: Thursday, January 23, 2003 7:44 AM
> > > > Subject: Re: XML validation using xml schema string
> > > >
> > > >
> > > > > I posted a question yesterday about the validation of XML against
> the
> > > > local
> > > > > schema (in memory). Could anyone please look at the message below
> and
> > > > reply
> > > > > if you know the answer.
> > > > >
> > > > > Thanks in Advance.
> > > > >
> > > > > Praveen
> > > > > ----- Original Message -----
> > > > > From: "Praveen Peddi" <ppeddi@contextmedia.com>
> > > > > To: <sandygao@ca.ibm.com>; <xerces-j-user@xml.apache.org>
> > > > > Sent: Wednesday, January 22, 2003 1:35 PM
> > > > > Subject: Re: XML validation using xml schema string
> > > > >
> > > > >
> > > > > > As you said I wrote an EntityResolver but it didn't work. Could
> you
> > > send
> > > > > me
> > > > > > a sample code (just the validation code) if u already have one.
> > > > > >
> > > > > > My validate method looks as follows:
> > > > > >
> > > > > > public static void validateXmlAgainstLocalSchema(String
XMLString,
> > > > String
> > > > > > schemaString) throws SAXException{
> > > > > > long startTime = System.currentTimeMillis();
> > > > > > System.out.println("Files: XML:" + XMLString + "
XSD:"
> +
> > > > > > schemaString);
> > > > > > XMLReader parser = null;
> > > > > > // Instantiate a parser
> > > > > > try{
> > > > > > parser =
> > > > > >
> > >
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
> > > > > > } catch (org.xml.sax.SAXException ex){
> > > > > > System.out.println("SAXException Exception");
> > > > > > throw new SAXException("Error Obtaining the parser.");
> > > > > > }
> > > > > >
> > > > > > try{
> > > > > >
> > > > > > // Register the error handler
> > > > > > parser.setErrorHandler(new
SchemaErrorHandler());
> > > > > >
> > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>
//parser.setFeature("http://apache.org/xml/features/continue-after-fatal-err
> > > > > > or", true);
> > > > > > // Turn on validation
> > > > > >
> > > > > >
parser.setFeature("http://xml.org/sax/features/namespaces",true);
> > > > > >
> > > > > parser.setFeature("http://xml.org/sax/features/validation",
> > > > > > true);
> > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
> > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>
parser.setFeature("http://apache.org/xml/features/validation/schema-full-che
> > > > > > cking",true);
> > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>
//parser.setProperty("http://apache.org/xml/properties/schema/external-noNam
> > > > > > espaceSchemaLocation",Schema);
> > > > > > parser.setEntityResolver(new
> > SchemaEntityResolver(schemaString));
> > > > > >
> > > > > >
> > > > > > } catch (org.xml.sax.SAXNotRecognizedException ex){
> > > > > > ex.printStackTrace();
> > > > > >
System.out.println("SAXNotRecognizedException
> > > > > > Exception");
> > > > > > } catch (org.xml.sax.SAXNotSupportedException ex){
> > > > > > ex.printStackTrace();
> > > > > > System.out.println("SAXNotSupportedException
> > > > > > Exception");
> > > > > > }
> > > > > >
> > > > > > // Parse the document
> > > > > > try{
> > > > > > //String XMLSystemId = new
> > > > > > File(XMLString).toURL().toExternalForm();
> > > > > > parser.parse(new InputSource(new
> > > > > StringReader(XMLString)));
> > > > > > //parser.parse(XMLSystemId);
> > > > > > System.out.println("Parsed Successfully by SAX
> > > Parser");
> > > > > > } catch (org.xml.sax.SAXException ex){
> > > > > > System.out.println("SAXException Exception");
> > > > > > throw new SAXException("Error Validating the content XML.");
> > > > > > } catch (java.io.IOException ex){
> > > > > > System.out.println("IOException Exception");
> > > > > > throw new SAXException("Error Validating the content XML.");
> > > > > > } finally {
> > > > > > long endTime = System.currentTimeMillis();
> > > > > > System.out.println("Total time of
> > > > > > SAXValidate:"+(endTime-startTime));
> > > > > > }
> > > > > > }//SAXValidate
> > > > > >
> > > > > > And SchemaEntityResolver looks as follows:
> > > > > >
> > > > > > public class SchemaEntityResolver implements EntityResolver {
> > > > > > InputSource source = null;
> > > > > > public SchemaEntityResolver(String schemaString) {
> > > > > > source = new InputSource(new StringReader(schemaString));
> > > > > > }
> > > > > >
> > > > > > public InputSource resolveEntity(String publicID, String
> > systemID)
> > > > > > throws SAXException {
> > > > > > return source;
> > > > > > }
> > > > > > }
> > > > > >
> > > > > > When I try to validate an xml with the schema string, I get the
> > > > following
> > > > > > error:
> > > > > > Line: 1
> > > > > > URI: null
> > > > > > Message: cvc-elt.1: Cannot find the declaration of element
> > > 'content'.
> > > > > >
> > > > > >
> > > > > > Am I doing it wrong? If so please tell me how to use the
> > > entityresolver.
> > > > > >
> > > > > > Praveen
> > > > > >
> > > > > >
> > > > > > ----- Original Message -----
> > > > > > From: <sandygao@ca.ibm.com>
> > > > > > To: <xerces-j-user@xml.apache.org>
> > > > > > Sent: Monday, January 20, 2003 10:18 AM
> > > > > > Subject: Re: XML validation using xml schema string
> > > > > >
> > > > > >
> > > > > > > Write an entity resolver
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>
(http://xml.apache.org/xerces2-j/javadocs/api/org/xml/sax/EntityResolver.htm
> > > > > > l),
> > > > > > >
> > > > > > > and returned an input source with a StringReader when the
schema
> > > > > document
> > > > > > > is asked.
> > > > > > >
> > > > > > > HTH,
> > > > > > > Sandy Gao
> > > > > > > Software Developer, IBM Canada
> > > > > > > (1-905) 413-3255
> > > > > > > sandygao@ca.ibm.com
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > "Praveen Peddi"
> > > > > > > <ppeddi@contextme To:
> > > > > > <xerces-j-user@xml.apache.org>
> > > > > > > dia.com> cc:
> > > > > > > Subject: Re:
XML
> > > > > > validation using xml schema string
> > > > > > > 01/20/2003 09:16
> > > > > > > AM
> > > > > > > Please respond to
> > > > > > > xerces-j-user
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > Hi all,
> > > > > > > Last week I sent an email regarding the xml validation against
> the
> > > > > schema,
> > > > > > > but didn't get any reply. Please reply if any one has answer
to
> my
> > > > > > > question. Is it possible to validate an xml document using a
> > schema
> > > > > > > document (not the schema url) that is present with in the
> > > application.
> > > > > If
> > > > > > > its not possible, whats the best way of achieving it. Please
> read
> > my
> > > > > > > previous email below.
> > > > > > >
> > > > > > > Thanks
> > > > > > > Praveen
> > > > > > >
> > > > > > > Hi all,
> > > > > > > I want to validate an xml document with an existing schema. I
> > know
> > > > > there
> > > > > > > is a code to do it when the schema is present as a URL. BUt
in
> my
> > > > case
> > > > > > > schema is present with in the application memory (since it is
> > > stored
> > > > in
> > > > > > > the database) and that is not the url or on the hard disk.
> > > > > > > I have the following method which validates only when the
> schema
> > is
> > > > > > > present externally. Note that the argument Schema is a URL:
> > > > > > > public static void DOMValidate(String XMLfile, String
Schema){
> > > > > > > long startTime = System.currentTimeMillis();
> > > > > > >
> > > > > > > // Instantiate the DOM parser.
> > > > > > > DOMParser parser = new DOMParser();
> > > > > > >
> > > > > > > // set the features
> > > > > > > try{
> > > > > > >
> > > parser.setFeature("http://xml.org/sax/features/namespaces
> > > > > > > ",true);
> > > > > > >
> > > > parser.setFeature("http://xml.org/sax/features/validation",
> > > > > > > true);
> > > > > > > parser.setFeature("
> > > > > > > http://apache.org/xml/features/validation/schema",true);
> > > > > > > parser.setFeature("
> > > > > > >
> > > >
http://apache.org/xml/features/validation/schema-full-checking",true);
> > > > > > > // convert the xml file name to a URL
> > > > > > > String SystemId = null;
> > > > > > > try{
> > > > > > > SystemId = new
> > > File(Schema).toURL().toExternalForm();
> > > > > > > } catch(java.net.MalformedURLException ex){
> > > > > > > //ex.printStackTrace();
> > > > > > > log.error(ex);
> > > > > > > }
> > > > > > > parser.setProperty("
> > > > > > >
> > > > > >
> > > >
> >
http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation
> > > > > > > ",SystemId);
> > > > > > > // myErrorHandler is a descendant of
ErrorHandler,
> it
> > > > > should
> > > > > > > be set here to be able to catch parse errors
> > > > > > > parser.setErrorHandler(new SchemaErrorHandler());
> > > > > > >
> > > > > > > } catch (SAXNotSupportedException ex){
> > > > > > > System.out.println("SAXNotSupportedException
> > > Exception");
> > > > > > > } catch (SAXNotRecognizedException ex){
> > > > > > > System.out.println("SAXNotRecognizedException
> > > > > > Exception");
> > > > > > > }
> > > > > > >
> > > > > > > // parse the xml file, the errorhandler class has
> > > callbacks,
> > > > > > > // so those will be called automatically there is a
> parse
> > > > error
> > > > > > > try{
> > > > > > > parser.parse(new
> > > > > File(XMLfile).toURL().toExternalForm());
> > > > > > > //System.out.println("Parsed Successfully by
> DOM
> > > > > > Parser");
> > > > > > > } catch (org.xml.sax.SAXException ex){
> > > > > > > //System.out.println("SAXException
Exception");
> > > > > > > //ex.printStackTrace();
> > > > > > > log.error(ex);
> > > > > > > } catch (java.io.IOException ex){
> > > > > > > System.out.println("IOException Exception");
> > > > > > > } finally {
> > > > > > > long endTime = System.currentTimeMillis();
> > > > > > > //System.out.println("Total time of DOMValidate:"
> > > > > > > +(endTime-startTime));
> > > > > > >
> > > > > > > }
> > > > > > >
> > > > > > > }
> > > > > > >
> > > > > > > How can I do the validation if the Schema is the actual
schema
> > > string
> > > > > not
> > > > > > > the url. This looks like a common problem. I assume that its
> not
> > > > > > universal
> > > > > > > that schema is not always stored externally.
> > > > > > >
> > > > > > > Any suggestions are appreciated.
> > > > > > >
> > > > > > > Praveen
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > >
> ---------------------------------------------------------------------
> > > > > > > To unsubscribe, e-mail:
xerces-j-user-unsubscribe@xml.apache.org
> > > > > > > For additional commands, e-mail:
> xerces-j-user-help@xml.apache.org
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > ---------------------------------------------------------------------
> > > > > > To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> > > > > > For additional commands, e-mail:
xerces-j-user-help@xml.apache.org
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > ---------------------------------------------------------------------
> > > > > To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> > > > > For additional commands, e-mail: xerces-j-user-help@xml.apache.org
> > > > >
> > > > >
> > > >
> > > >
> > >
> ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> > > > For additional commands, e-mail: xerces-j-user-help@xml.apache.org
> > > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> > > For additional commands, e-mail: xerces-j-user-help@xml.apache.org
> > >
> > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xerces-j-user-help@xml.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic