  | |  | XML validation using xml schema string | XML validation using xml schema string 2003-01-20 - By McEvoy, Peter
I saw a thing in the ant documentation which you may be able to pick up a few tips from - it sounds like what you are trying to do (although they validate against DTDs). You may need to peek at their code to see how to do it... http://jakarta.apache.org/ant/manual/OptionalTasks/xmlvalidate.html Pete
-----Original Message----- From: Praveen Peddi [mailto:ppeddi@(protected)] Sent: 20 January 2003 14:17 To: xerces-j-user@(protected) Subject: Re: XML validation using xml schema string
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 <http://xml.org/sax/features/namespaces> ); parser.setFeature(" http://xml.org/sax/features/validation", true); parser.setFeature(" http://apache.org/xml/features/validation /schema",true <http://apache.org/xml/features/validation/schema> ); parser.setFeature(" http://apache.org/xml/features/validation /schema-full-checking",true <http://apache.org/xml/features/validation/schema -full-checking> ); // 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 <http://apache.org/xml/properties /schema/external-noNamespaceSchemaLocation> ); // 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2722.900" name=GENERATOR> <STYLE></STYLE> </HEAD> <BODY bgColor=#ffffff> <DIV><SPAN class=023095314-20012003><FONT face=Arial color=#0000ff size=2>I saw a thing in the ant documentation which you may be able to pick up a few tips from - it sounds like what you are trying to do (although they validate against DTDs). You may need to peek at their code to see how to do it...</FONT></SPAN></DIV> <DIV><SPAN class=023095314-20012003><FONT face=Arial color=#0000ff size=2></FONT></SPAN> </DIV> <DIV><SPAN class=023095314-20012003><FONT face=Arial color=#0000ff size=2><A href="http://jakarta.apache.org/ant/manual/OptionalTasks/xmlvalidate.html">http ://jakarta.apache.org/ant/manual/OptionalTasks/xmlvalidate.html</A></FONT></SPAN ></DIV> <DIV><SPAN class=023095314-20012003><FONT face=Arial color=#0000ff size=2></FONT></SPAN> </DIV> <DIV><SPAN class=023095314-20012003><FONT face=Arial color=#0000ff size=2>Pete</FONT></SPAN></DIV> <BLOCKQUOTE dir=ltr style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px"> <DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Tahoma size=2>-----Original Message-----<BR><B>From:</B> Praveen Peddi [mailto:ppeddi@(protected)]<BR><B>Sent:</B> 20 January 2003 14:17<BR><B>To:</B> xerces-j-user@(protected)<BR><B>Subject:</B> Re: XML validation using xml schema string<BR><BR></FONT></DIV> <DIV><FONT face=Arial size=2>Hi all,</FONT></DIV> <DIV><FONT face=Arial size=2>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.</FONT></DIV> <DIV><FONT face=Arial size=2></FONT> </DIV> <DIV><FONT face=Arial size=2>Thanks</FONT></DIV> <DIV><FONT face=Arial size=2>Praveen</FONT></DIV> <DIV><BR></DIV> <BLOCKQUOTE dir=ltr style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px"> <DIV><FONT face=Arial size=2> <DIV><FONT face=Arial size=2>Hi all,</FONT></DIV> <DIV><FONT face=Arial size=2>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.</FONT></DIV> <DIV><FONT face=Arial size=2>I have the following method which validates only when the schema is present externally. Note that the argument Schema is a URL:</FONT></DIV> <DIV><FONT face=Arial size=2>public static void DOMValidate(String XMLfile, String Schema){<BR> long startTime = System.currentTimeMillis(); </FONT></DIV> <DIV> </DIV> <DIV><FONT face=Arial size=2> // Instantiate the DOM parser.<BR> DOMParser parser = new DOMParser();</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Arial size=2> // set the features<BR> try{<BR> parser.setFeature("<A href='http://xml.org/sax/features/namespaces",true'>http://xml.org/sax /features/namespaces",true</A>);<BR> parser.setFeature("<A href="http://xml.org/sax/features/validation">http://xml.org/sax/features /validation</A>", true);<BR>   ; parser.setFeature("<A href='http://apache.org/xml/features/validation/schema",true'>http://apache .org/xml/features/validation/schema",true</A>);<BR>   ; parser.setFeature("<A href='http://apache.org/xml/features/validation/schema-full-checking",true' >http://apache.org/xml/features/validation/schema-full-checking",true</A>);<BR> // convert the xml file name to a URL<BR> String SystemId = null;<BR> try{<BR> SystemId = new File(Schema).toURL().toExternalForm();<BR> } catch(java.net.MalformedURLException ex){<BR> //ex.printStackTrace();<BR> log.error(ex);<BR> }<BR> parser.setProperty("<A href='http://apache.org/xml/properties/schema/external -noNamespaceSchemaLocation",SystemId'>http://apache.org/xml/properties/schema /external-noNamespaceSchemaLocation",SystemId</A>);<BR> // myErrorHandler is a descendant of ErrorHandler, it should be set here to be able to catch parse errors<BR>   ; parser.setErrorHandler(new SchemaErrorHandler());</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Arial size=2> } catch (SAXNotSupportedException ex){<BR> System.out.println("SAXNotSupportedException Exception");<BR> } catch (SAXNotRecognizedException ex){<BR> System.out.println("SAXNotRecognizedException Exception");<BR> }</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Arial size=2> // parse the xml file, the errorhandler class has callbacks,<BR> // so those will be called automatically there is a parse error<BR> try{<BR> parser.parse(new File(XMLfile).toURL().toExternalForm());<BR> //System.out.println("Parsed Successfully by DOM Parser");<BR> } catch (org.xml.sax.SAXException ex){<BR> //System.out.println("SAXException Exception");<BR>   ; //ex.printStackTrace();<BR> log.error(ex);<BR> } catch (java.io.IOException ex){<BR> System.out.println("IOException Exception");<BR> } finally {<BR> long endTime = System.currentTimeMillis();<BR> //System.out.println("Total time of DOMValidate:"+(endTime-startTime));<BR> <BR> }</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Arial size=2> }</FONT></DIV> <DIV><FONT face=Arial size=2></FONT> </DIV> <DIV><FONT face=Arial size=2>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.</FONT></DIV> <DIV><FONT face=Arial size=2></FONT> </DIV> <DIV><FONT face=Arial size=2>Any suggestions are appreciated.</FONT></DIV> <DIV><FONT face=Arial size=2></FONT> </DIV> <DIV><FONT face=Arial size=2>Praveen</FONT></DIV></FONT></DIV></BLOCKQUOTE></BLOCKQUOTE></BODY></HTML>
|
|
 |