[jdom-interest] XML Schema validation asking or DOCTYPE?

Neil Blue neilblue at bigfoot.com
Thu Oct 25 01:26:05 PDT 2001


Hello,

I am using jdom b7, and I still have the same problem I have been 
working on for three days now. I can't get jdom to validate an 
XMLSchema. I have had quite a bit of help getting the schema files set 
up (thanks to, Jacqueline), but still the validation fails. Here is my code:

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

public class SAXBuilderDemo
{
   public static void main( String[] args )
       throws Exception
   {
       SAXBuilder builder = new SAXBuilder(true);
       Document doc = builder.build( "contents.xml" );
       System.err.println( doc );
   }
}

my xml file has:

<?xml version="1.0"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="myschema.xsd">
   <title>Test string</title>
   <rubbish></rubbish>
</Book>

the myschema.xsd file contains:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault="unqualified">
   <xsd:element name="Book">
       <xsd:complexType>
           <xsd:sequence>
               <xsd:element ref="title"/>
           </xsd:sequence>
       </xsd:complexType>
   </xsd:element>

   <xsd:element name="title">
       <xsd:simpleType>
           <xsd:restriction base="xsd:string"/>
       </xsd:simpleType>
   </xsd:element>
</xsd:schema>

(this has been verified for me using XMLSpy, thanks to, Dan Feather)

However when I run the app, I get the error:

Exception in thread "main" org.jdom.JDOMException: Error on line 3 of 
document file:///home/neil/jdom/samples/contents.xml: Element type 
"Book" must be declared.
   at org.jdom.input.SAXBuilder.build(SAXBuilder.java:358)
...

running with jdk1.3 and xerces.jar supplied with jdom b7.

or running with jdk1.4.0 b2:

Exception in thread "main" org.jdom.JDOMException: Error on line 2 of 
document file:/home/neil/jdom/samples/contents.xml: Element type "Book" 
is not declared. at org.jdom.input.SAXBuilder.build(SAXBuilder.java:358) 
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:513)
...

or running with jdk1.3.1 using xerces 2.0.0 b2

Exception in thread "main" org.jdom.JDOMException: Error in building: 
Document root element "Book", must match DOCTYPE root "null".
   at org.jdom.input.SAXBuilder.build(SAXBuilder.java:375)
...

please could anyone shed some light on what I am doing wrong, or 
possibly verify this output.

Thank you
Neil Blue




More information about the jdom-interest mailing list