Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Validate Document object against Schema without reparsing the xml

Thread view: 
veerleverbr@hotmail.com - 22 Jun 2005 08:55 GMT
Hi,

Is it possible to validate a Document object against a schema file
without reparsing the xml:

public void validate(org.w3c.dom.Document doc, File xsd) throws
SAXException {
  ???
}

Any suggestions?

Veerle
iksrazal@terra.com.br - 23 Jun 2005 00:48 GMT
> Is it possible to validate a Document object against a schema file
> without reparsing the xml:
[quoted text clipped - 7 lines]
>
> Veerle

Just load the objects into a static variable, singleton etc. Use
EntityResolver for the schema. DefaultHandler is the way to go for the
validation. Try clarifying the problem if I mis-understand.

HTH,
iksrazal
www.braziloutsource.com
veerleverbr@hotmail.com - 28 Jun 2005 08:42 GMT
I found it is possible to do such a thing using the
javax.xml.validation package of Java 1.5:

private class ValidationErrorHandler extends DefaultHandler {
   private List exList = new ArrayList();
   public SAXParseException getFirstSaxParseException() {
       return
(exList.size()==0?null:(SAXParseException)exList.get(0));
   }
   public boolean validationError() {
       return (exList.size()> 0);
   }
   public void error(SAXParseException exception) throws SAXException
{
       exList.add(exception);
   }
   public void fatalError(SAXParseException exception) throws
SAXException {
       exList.add(exception);
   }
   public void warning(SAXParseException exception) throws
SAXException {
   }
}

public void validateDocument(Document doc, String xsd) throws
SAXException, IOException {
   SchemaFactory factory = SchemaFactory.newInstance

(XMLConstants.W3C_XML_SCHEMA_NS_URI);
   Source schemaFile = new StreamSource(new File(xsd));
   Schema schema = factory.newSchema(schemaFile);
   Validator validator = schema.newValidator();
   ValidationErrorHandler errHandler = new ValidationErrorHandler();
   validator.setErrorHandler(errHandler);
   validator.validate(new DOMSource(doc));
   if (errHandler.validationError()) {
       throw errHandler.getFirstSaxParseException();
   }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.