RE: XML Schema validation using JAXP Q?

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 {
            parser.setFeature
            ("http://xml.org/sax/features/validation", true);
            parser.setFeature 
            ("http://apache.org/xml/features/validation/schema",true);
            parser.setFeature
 
("http://apache.org/xml/features/validation/schema-full-checking",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 {
            parser.parse(xmlFile);
//            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]);
    }
}



Tom

-----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
needed):

C:\xml>javac -classpath
.;c:\thirdparty\apache\xerces.jar;c:\thirdparty\apache\x
ml-apis.jar SchemaValidate.java

C:\xml>java -classpath
.;c:\thirdparty\apache\xerces.jar;c:\thirdparty\apache\xm
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):

------
editor.xml
------
<!DOCTYPE htmlEditor >
<htmlEditor
  xmlns="http://www.emilygraham.com/java/other/Editor"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.emilygraham.com/java/other/Editor
  http://www.emilygraham.com/java/other/editor.xsd">
   
  <updateTime>1999-05-31T13:20:00</updateTime>
  <fields>
    <field columnName="nickname">
      <shortDesc>Short Desc</shortDesc>
      <htmlType name="select">
        <name>firstName</name>
        <value>lastName</value>
      </htmlType>
      <allowNull>tru</allowNull>
      <defaultVal>Blueberry</defaultVal>
    </field>
  </fields>
</htmlEditor>

------
editor.xsd
------

<schema targetNamespace="http://www.emilygraham.com/java/other/Editor"
        xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:e="http://www.emilygraham.com/java/other/Editor"
        elementFormDefault="qualified">        

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

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

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

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

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

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

------
hope this helps.

TOm

-----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,

Jaub

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