[jdom-interest] Re: XML Schema Validation

Richard Baldwin baldwin at austin.cc.tx.us
Wed May 9 06:25:18 PDT 2001

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

    Document document = null;
    try {
      SAXBuilder saxBuilder = 
                      new SAXBuilder(true);
      document = 
             saxBuilder.build(new File(
    }catch(Exception e){
      System.out.println("Exception during build");
      System.out.println("End stack trace");
    }//end catch

"Kenworthy, Edward" wrote:
> Hi Larry
> Ah !    That's interesting, thanks. What happens if the XML document doesn't
> conform to the schema ? Does the builder give up or do you end up with a
> Document that is flagged as non-conforming that you can still work with ? (I
> would prefer the latter as in my app whilst documents do have schemas it is
> valid for them to be non-conforming as in incomplete (eg Element foo should
> have child bar, but it doesn't yet) but not for them to be invalid (eg
> Element foo cannot have a child wibble.) Uhm, hope that made sense.
> Edward
> -----Original Message-----
> From: Larry Davies [mailto:Larry_Davies at notes.compaid.com]
> Sent: 09 May 2001 13:15
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] Re: XML Schema Validation
> To answer the questions some are having regarding using schemas, the answer
> is
> Yes/No.  As others have mentioned, JDOM does not in and of itself validate
> schemas.  The underlying parser (like Xerces) validates the xml document
> against
> a schema.  I recently had some problems getting my XML document to parse and
> validate against a schema using JDOM Beta 6.  I got it to work now.  You
> have to
> be using a version of a parser that will validate with schemas.  I used
> Xerces
> version 1.3.0.  FYI, the older version of Xerces 1.2.0 does not work for
> schema
> validation.  To validate the document using JDOM, I believe you must
> instantiate
> the builder as follows: SAXBuilder builder = new SAXBuilder(true).  In
> addition,
> you must have your XML document point to your schema.  The following is a
> snippet from my XML document and Schema document.
> Schema:
> <?xml version="1.0" encoding="UTF-8"?>
> <schema xmlns="http://www.w3.org/2000/10/XMLSchema"
>         xmlns:cs="http://localhost"
>         elementFormDefault="qualified"
>         attributeFormDefault="unqualified"
>         targetNamespace="http://localhost">
> XML document:
> <Data xmlns="http://localhost"
>       xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
>       xsi:schemaLocation="http://localhost mySchema.xsd">
> The localhost reference could be your domain name and the mySchema.xsd is
> the
> name for your schema document.  I believe you may also need to tell JDOM
> to use the namespace http://localhost.  Not 100% sure on that, but I believe
> it
> did not work for me until I did that.
> Anyway, there may be other ways, but this worked fine for me.
> Thanks.
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
> t.com
> _______________________________________________
> 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

Professor of Computer Studies
Austin Community College
(512) 223-4758 or (512) 250-8682
mailto:baldwin.richard at iname.com  

More information about the jdom-interest mailing list