[jdom-interest] Re: XML Schema Validation

Richard Baldwin baldwin at austin.cc.tx.us
Thu May 10 07:15:35 PDT 2001


I don't disagree with your statement.  However, some
elaboration may be beneficial for those who are
interested in learning more about the behavior of JDOM
in the event of a validation error.

My original statement as quoted below is correct for my
test case and my test program.

The error in my test case was a validation error (not a
well-formed error) consisting of a missing required
attribute.  This resulted in the following exception
being thrown:

Exception in build
org.jdom.JDOMException: Error on line 15 of 
document file:/C:/jnk/Jdom04InValid.x
ml: Attribute "DummyAttribute" is required 
and must be specified for element type "poem".

Because the exception was thrown by a statement inside
the try-block that reads:

  document = saxBuilder.build(new File(filename));

control was transferred to the catch-block.  This left
the contents of the reference variable named document
as null (no reference to a Document object).

However, perhaps I should have implemented an error
handler to intercept the validation error and thus
prevent control from being transferred to the catch
block.  Probably in that case, I could have evaluated
the significance of the problem in my error handler and
could have allowed the build to continue if that was
appropriate.

For the benefit of those who may not be familiar with
the implementation of error handlers using callback
methods, the JDOM class named BuilderErrorHandler
provides the following three callback methods that can
be overridden to implement custom behavior in the event
of an error, a fatalError, or a wraning:

void error(org.xml.sax.SAXParseException exception) 
This method is called in response to an error that has
occurred; this indicates that a rule was broken,
typically in
validation, but that parsing could reasonably continue.

void fatalError(org.xml.sax.SAXParseException
exception) 
This method is called in response to a fatal error;
this
indicates that a rule has been broken that makes
continued parsing
either impossible or an almost certain waste of time.

void warning(org.xml.sax.SAXParseException exception) 
This method is called when a warning has occurred; this
indicates that while no XML rules were broken,
something
appears to be incorrect or missing.

Dick Baldwin

Andy Clark wrote:
> 
> Richard Baldwin wrote:
> > On a validation error (DTD or schema), the following
> > code throws an exception and does not build the
> > Document object.  The Document reference contains null
> > when the execution of the catch block completes. - Dick
> > Baldwin
> 
> Validation errors are non-fatal and do not throw a
> parse exception. Xerces will stop parsing only in the
> case of fatal errors that are caused by the document
> not being well-formed.
> 
> --
> Andy Clark * IBM, TRL - Japan * andyc at apache.org
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com

-- 
=========================================================
Richard G. Baldwin (Dick Baldwin)
Home of Baldwin's on-line Java Tutorials
http://www.geocities.com/Athens/7077/scoop/onjava.html

Professor of Computer Studies
Austin Community College
(512) 223-4758 or (512) 250-8682
mailto:baldwin.richard at iname.com  
http://www2.austin.cc.tx.us/baldwin/
---------------------------------------------------------



More information about the jdom-interest mailing list