You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-user@xml.apache.org by Praveen Peddi <pp...@contextmedia.com> on 2003/01/15 18:36:09 UTC
How to validate an xml document with the existing schema.
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 (since it is stored in the database) and that is not the url.
I have the following code:
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
Re: How to validate an xml document with the existing schema.
Posted by Praveen Peddi <pp...@contextmedia.com>.
Thanks Scott.
I will do that.
Praveen
----- Original Message -----
From: "Scott Nichol" <sn...@scottnichol.com>
To: <so...@xml.apache.org>
Sent: Wednesday, January 15, 2003 1:35 PM
Subject: Re: How to validate an xml document with the existing schema.
> Have you tried posting this question to a Xerces list? The folks
> there probably know most about parsing than the SOAPers here.
>
> On 15 Jan 2003 at 12:36, Praveen Peddi wrote:
>
> >
> > 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 (since it is stored
> > in the database) and that is not the url.
> > I have the following code:
> > 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(");
> >
> > parser.setFeature("http://xml.org/sax/features/validation", true);
> > parser.setFeature(");
> > parser.setFeature(");
> > // 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(");
> > // 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
>
>
> Scott Nichol
>
>
> --
> To unsubscribe, e-mail: <ma...@xml.apache.org>
> For additional commands, e-mail: <ma...@xml.apache.org>
>
--
To unsubscribe, e-mail: <ma...@xml.apache.org>
For additional commands, e-mail: <ma...@xml.apache.org>
Re: How to validate an xml document with the existing schema.
Posted by Praveen Peddi <pp...@contextmedia.com>.
Thanks Scott.
I will do that.
Praveen
----- Original Message -----
From: "Scott Nichol" <sn...@scottnichol.com>
To: <so...@xml.apache.org>
Sent: Wednesday, January 15, 2003 1:35 PM
Subject: Re: How to validate an xml document with the existing schema.
> Have you tried posting this question to a Xerces list? The folks
> there probably know most about parsing than the SOAPers here.
>
> On 15 Jan 2003 at 12:36, Praveen Peddi wrote:
>
> >
> > 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 (since it is stored
> > in the database) and that is not the url.
> > I have the following code:
> > 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(");
> >
> > parser.setFeature("http://xml.org/sax/features/validation", true);
> > parser.setFeature(");
> > parser.setFeature(");
> > // 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(");
> > // 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
>
>
> Scott Nichol
>
>
> --
> To unsubscribe, e-mail: <ma...@xml.apache.org>
> For additional commands, e-mail: <ma...@xml.apache.org>
>
Re: How to validate an xml document with the existing schema.
Posted by Scott Nichol <sn...@scottnichol.com>.
Have you tried posting this question to a Xerces list? The folks
there probably know most about parsing than the SOAPers here.
On 15 Jan 2003 at 12:36, Praveen Peddi wrote:
>
> 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 (since it is stored
> in the database) and that is not the url.
> I have the following code:
> 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(");
>
> parser.setFeature("http://xml.org/sax/features/validation", true);
> parser.setFeature(");
> parser.setFeature(");
> // 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(");
> // 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
Scott Nichol
Re: How to validate an xml document with the existing schema.
Posted by Scott Nichol <sn...@scottnichol.com>.
Have you tried posting this question to a Xerces list? The folks
there probably know most about parsing than the SOAPers here.
On 15 Jan 2003 at 12:36, Praveen Peddi wrote:
>
> 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 (since it is stored
> in the database) and that is not the url.
> I have the following code:
> 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(");
>
> parser.setFeature("http://xml.org/sax/features/validation", true);
> parser.setFeature(");
> parser.setFeature(");
> // 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(");
> // 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
Scott Nichol
--
To unsubscribe, e-mail: <ma...@xml.apache.org>
For additional commands, e-mail: <ma...@xml.apache.org>