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