W3C home > Mailing lists > Public > xmlschema-dev@w3.org > December 2001

RE: XML Schema validation using JAXP Q?

From: Tom Preston <tpreston@amadeusboston.com>
Date: Mon, 17 Dec 2001 16:26:56 -0500
Message-ID: <498B0C8F9141D511927B009027B113873A0F18@email-temp.vacation-corp.com>
To: Jakub.Valenta@Deio.net, xmlschema-dev@w3.org
Oops...maybe you would also like the code for the SchemaValidate.java
program.  Here it is:

import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.ErrorHandler;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.io.IOException;

//  A Valdating DOM Application
//  with registered Error Handlers
public class SchemaValidate implements ErrorHandler {
    // Constructor
    public SchemaValidate (String xmlFile) {
        //  Create a Xerces DOM Parser
        DOMParser parser = new DOMParser();
        //  Turn Validation on
        try {
            ("http://xml.org/sax/features/validation", true);

        } catch (SAXNotRecognizedException e) {
            System.err.println (e);
        } catch (SAXNotSupportedException e) {
            System.err.println (e);
        //  Register Error Handler
        parser.setErrorHandler (this);

        //  Parse the Document      
        //  and traverse the DOM
        try {
//            Document document = parser.getDocument();
//            traverse (document);
        } catch (SAXException e) {
            System.err.println (e);
        } catch (IOException e) {
            System.err.println (e);
        } catch (Exception e) {
            System.err.println (e);
    //  Traverse DOM Tree.  Print out Element Names
    private void traverse (Node node) {
        int type = node.getNodeType();
        if (type == Node.ELEMENT_NODE)
            System.out.println (node.getNodeName());
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i=0; i< children.getLength(); i++) 
                traverse (children.item(i));    
    //  Warning Event Handler
    public void warning (SAXParseException e)
        throws SAXException {
        System.err.println ("Warning:  "+e);

    //  Error Event Handler
    public void error (SAXParseException e)
        throws SAXException {
        System.err.println ("Error:  "+e);
    //  Fatal Error Event Handler
    public void fatalError (SAXParseException e)
        throws SAXException {
        System.err.println ("Fatal Error:  "+e);
    // Main Method
    public static void main (String[] args) {
        SchemaValidate validatingDOM = 
        	new SchemaValidate (args[0]);


-----Original Message-----
From: Tom Preston 
Sent: Monday, December 17, 2001 3:57 PM
To: Jakub.Valenta@Deio.net; xmlschema-dev@w3.org
Subject: RE: XML Schema validation using JAXP Q?

You write a trivial java program like this:

Compile and run it like this (notice xerces.jar and xml-apis.jar are

C:\xml>javac -classpath
ml-apis.jar SchemaValidate.java

C:\xml>java -classpath
l-apis.jar SchemaValidate editor99.xml
Error:  org.xml.sax.SAXParseException: Datatype error: In element
'allowNull' :
tru is not a boolean.

NOtice that editor99.xml failed validation.  Here is editor99.xml and
editor.xsd which it validates against (note the xsi:schemaLocation is
defined in the .xml file):

<!DOCTYPE htmlEditor >
    <field columnName="nickname">
      <shortDesc>Short Desc</shortDesc>
      <htmlType name="select">


<schema targetNamespace="http://www.emilygraham.com/java/other/Editor"

    <documentation xml:lang="en">
     Editor document definition defines an html based editor

  <element name="htmlEditor" type="e:HtmlEditorType"/>

  <element name="updateTime" type="dateTime"/>

  <complexType name="HtmlEditorType">
      <element ref="e:updateTime" minOccurs="1" maxOccurs="1" />
      <element name="fields" type="e:Fields" minOccurs="1" maxOccurs="1" />

  <complexType name="Fields">
      <element name="field" minOccurs="1" maxOccurs="unbounded">
            <element name="shortDesc" type="string" minOccurs="1"
            <element name="htmlType" type="e:HtmlTypeType" minOccurs="1"
            <element name="allowNull" type="boolean" minOccurs="1"
maxOccurs="1" />
            <element name="defaultVal"  type="string"/>
          <attribute name="columnName" type="string" use="required"/>

  <complexType name="HtmlTypeType">
      <element name="name" type="string" /> 
      <element name="value" type="string" /> 
    <attribute name="name" type="e:HtmlNameType" use="required"/>  
  <simpleType name="HtmlNameType">
    <restriction base="NMTOKEN">   
      <enumeration value="select"/>
      <enumeration value="text"/>

hope this helps.


-----Original Message-----
From: Jakub.Valenta@Deio.net [mailto:Jakub.Valenta@Deio.net]
Sent: Monday, December 17, 2001 12:26 PM
To: xmlschema-dev@w3.org
Subject: XML Schema validation using JAXP Q?

Hi all,
I would like to ask if it is possible to validate xml document against XML
Schema using JAXP? At the moment I am using XALAN and it should be XML
Schema aware, but I have no clue how to do the vlidation using JAXP.

Any help appreciated,

Received on Monday, 17 December 2001 16:26:43 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:55:54 UTC