Hi, here is some code you can use, I routinely validate larger files. You can also extend your virtual memory if you need too.
****************** private String SCHEMA = "http://apache.org/xml/features/validation/schema"; /** * String constant used for the schema full checking feature of the Xerces * parser. * @build 10 */ private String SCHEMA_FULL_CHECKING = "http://apache.org/xml/features/validation/schema-full-checking"; /** * String constant used for the warn on duplicate attribute definition * feature of the Xerces parser. * @build 10 */ private String WARN_ON_DUPLICATE_ATTDEF = "http://apache.org/xml/features/validation/warn-on-duplicate-attdef"; /** * String constant used for the warn on undeclared element definition * feature of the Xerces parser. * @build 10 */ private String LOAD_EXTERNAL_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd"; first set up your parser I do it in my constructor: //Name of the parser to use to create the xmlReader. String parserName = "org.apache.xerces.parsers.SAXParser"; parser = XMLReaderFactory.createXMLReader(parserName); // load schema\dtd parser.setFeature(LOAD_EXTERNAL_DTD, true); // turn on validation parser.setFeature(VALIDATION, true); parser.setFeature(SCHEMA, true); parser.setFeature(SCHEMA_FULL_CHECKING, true); parser.setFeature(WARN_ON_DUPLICATE_ATTDEF, true); defaultHandler = new SAXErrorHandler(); ************************** next you parse the file using a handler that you create, you do not have to use an input source you use the path to the file, also don't have to setEntityResolver if the path to the schema is correct in the XML file. public void validateFile(InputSource source, DefaultHandler handler) throws SAXException, IOException { // make schema resolver so the parser knows where to find the schema SchemaResolver resolver = new SchemaResolver(); parser.setEntityResolver(resolver); parser.setErrorHandler(handler); //parsing the file with validation turned on no content handler needed parser.parse(source); } *************************** finaly I create an error handler, I do this as an internal class but it can be another class; /** * This class is used to handle any errors that occur durring parsing. * @build 10 */ class SAXErrorHandler extends DefaultHandler { /** * Called by parser when an error is encountered while parsing the XML * file. * @build 10 * @param ex is th exception that describes the error */ public void error(SAXParseException ex) { parseError.append( "\n" ); parseError.append( ex.getMessage()); errorCount++; } /** * Called by parser when an fatalError is encountered while parsing the * XML file. * @build 10 * @param ex is th exception that describes the error */ public void fatalError(SAXParseException ex) { parseError.append( "\n" ); parseError.append( ex.getMessage()); errorCount++; } /** * Called by parser when an warning is encountered while parsing the * XML file. * @build 10 * @param ex is th exception that describes the error */ public void warning(SAXParseException ex) { parseError.append( "\n" ); parseError.append( ex.getMessage()); errorCount++; } } Thanks, Doug -----Original Message----- From: Shital Joshi [mailto:[EMAIL PROTECTED] Sent: Tuesday, April 22, 2003 12:01 PM To: [EMAIL PROTECTED] Subject: RE: schema validation using xerces sax parser Hi Ragunath, Thanks for the reply. I appreciate that. The code which you sent is using the DOM parser. My XML document is around 20MB (in some cases it could be more than that). If I use DOM parser my computer will go out of virtual memory. So I am looking for a SAX parser solution to this problem. Thanks again, -Shital Joshi 732-560-0010 x215 MFG Systems -----Original Message----- From: Ragunath Marudhachalam [mailto:[EMAIL PROTECTED] Sent: Tuesday, April 22, 2003 11:53 AM To: [EMAIL PROTECTED] Subject: RE: schema validation using xerces sax parser Hi shital, This is a part i use to validate my xml with the schemas... Here i write the xml document to a temp file and then read it again. I'm doing this bcoz i had some problems.. .But u dont have to write to temp file, if u can validate directory from the doc object... Hope this helps... public boolean validateXml(Document doc) { try { File f = File.createTempFile("tempdoc", null, null); FileOutputStream fos = new FileOutputStream(f); try { OutputFormat format = new OutputFormat( doc); StringWriter stringOut = new StringWriter(); XMLSerializer serial = new XMLSerializer(stringOut, format ); serial.asDOMSerializer(); serial.serialize( doc.getDocumentElement()); fos.write((stringOut.toString()).getBytes()); }catch(Exception exp) { return false; } FileInputStream ins = new FileInputStream(f); InputSource insrc = new InputSource(ins); DOMParser parser = new DOMParser(); parser.setFeature( "http://xml.org/sax/features/validation",true); parser.setFeature( "http://xml.org/sax/features/namespaces",true ); parser.setFeature( "http://apache.org/xml/features/validation/schema",true); parser.setFeature("http://apache.org/xml/features/validation/schema-full-che cking",true); CvErrHandler err_handler = new CvErrHandler(); parser.setErrorHandler(err_handler); parser.parse(insrc); ins.close(); fos.close(); f.delete(); if (err_handler.getErrorStatus()) { Vector v = null; v = err_handler.getErrorMessages(); return false; } else { return true; } } catch(Exception e){Logger.global.log(Level.SEVERE, "error in writing xml" + e.getMessage()); return false; } } public class CvErrHandler extends DefaultHandler { File fLog = null; FileOutputStream fos = null; DataOutputStream dos = null; public boolean error = false; public Vector errvector = null; private static final String CVxVersion = "2.0"; public CvErrHandler() { errvector = new Vector(); } public boolean getErrorStatus() { return error; } public Vector getErrorMessages() { return errvector; } public void error(SAXParseException spe) { error = true; errvector.addElement("line #" + spe.getLineNumber() + ": " + spe.toString ()); } public void warning(SAXParseException spe) { error = true; errvector.addElement("line #" + spe.getLineNumber() + ": " + spe.toString ()); } public void fatalError(SAXParseException spe) throws org.xml.sax.SAXException { error = true; errvector.addElement("line #" + spe.getLineNumber() + ": " + spe.toString ()); throw new org.xml.sax.SAXException(spe); } } Ragu CircuitVision -----Original Message----- From: Shital Joshi [mailto:[EMAIL PROTECTED] Sent: Tuesday, April 22, 2003 11:42 AM To: [EMAIL PROTECTED] Subject: RE: schema validation using xerces sax parser Hi there, I searched web for XML Schema validation examples which uses Xerces SAX parser. The XML file which I am gonna use, is around 20 MB in size. I saw couple of examples which uses DOM parser. Can anyone point to any example or sample code which uses SAX parser for schema validation? Thanks in advance, -Shital Joshi MFG Systems -----Original Message----- From: Glenn Barnard [mailto:[EMAIL PROTECTED] Sent: Tuesday, April 22, 2003 11:07 AM To: [EMAIL PROTECTED] Subject: Re: schema validation using xerces sax parser Try using a Parser Configuration and register a error handler with it. >From: "Shital Joshi" <[EMAIL PROTECTED]> >Reply-To: [EMAIL PROTECTED] >To: <[EMAIL PROTECTED]> >Subject: schema validation using xerces sax parser >Date: Tue, 22 Apr 2003 10:11:03 -0400 > >Hi there, > >I am trying to validate a 20MB XML document againt a schema. I want to find >out portions of that xml document which doesn't validate against that >schema. How can I do that? > >-Shital Joshi >MFG Systems > > > >--------------------------------------------------------------------- >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] > _________________________________________________________________ MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*. http://join.msn.com/?page=features/virus --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]