Article:
  XML Document Validation with an XML Schema
Subject:   Handler exception
Date:   2006-06-14 02:35:57
From:   nnc
i am trying to use jdk1.4 SAXParserFactory and with setValidating(true) it doesnot recognize the attributes or xml tags.


When this code is run with Catalog.xml as argument, the error is "true Attribute "date" is not declared for element "journal"".


import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;


public class SchemaValidator1
{
public void validateSchema(String XmlDocumentUrl)
{
SAXParserFactory factory;
try{
Validator handler = new Validator();
factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(XmlDocumentUrl, handler);

if(handler.validationError==true)
System.out.println("XML Document has Error:"+handler.validationError +" " + handler.saxParseException.getMessage());
else
System.out.println("XML Document is valid"); }
catch(java.io.IOException ioe){
System.out.println("IOException"+ioe.getMessage());
}catch (SAXException se) {
System.out.println("SAXException"+se.getMessage()); }
catch(Exception e){
System.out.println("Exception"+e.getMessage()); }
}


private class Validator extends DefaultHandler
{
public boolean validationError = false;
public SAXParseException saxParseException=null;
public void error(SAXParseException exception) throws SAXException
{
validationError=true;
saxParseException=exception;
}
public void fatalError(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception) throws SAXException
{
validationError = false;
saxParseException=exception;
}
}


public static void main(String[] argv)
{
String XmlDocumentUrl=argv[0];


SchemaValidator1 validator=new SchemaValidator1();
validator.validateSchema(XmlDocumentUrl);
}
}


I would greatly appreciate if you can look into this ..

Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • Handler exception
    2006-06-14 07:23:45  Deepak Vohra | [View]

    Please post the XML document and XML Schema.
    • Handler exception
      2006-06-14 21:44:49  nnc [View]

      hi Deepak,

      Many thanks for looking in to this. The xml and schema was donloaded from your site, here it is. infact it gives the same exception for all the elements in the xml file. (i tried removing the attribute)

      <?xml version="1.0" encoding="UTF-8"?>
      <!--A OnJava Journal Catalog-->

      <catalog
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation=
      "file://c:/Schemas/catalog.xsd"
      title="OnJava.com" publisher="O'Reilly">
      <journal date="April 2004">
      <article>
      <title>Declarative Programming in Java</title>
      <author>Narayanan Jayaratchagan</author>
      </article>
      </journal>
      </catalog>

      schema,

      <?xml version="1.0" encoding="utf-8"?>
      <xs:schema
      xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="catalog">
      <xs:complexType>
      <xs:sequence>
      <xs:element ref="journal" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="title" type="xs:string"/>
      <xs:attribute name="publisher" type="xs:string"/>
      </xs:complexType>
      </xs:element>
      <xs:element name="journal">
      <xs:complexType>
      <xs:sequence>
      <xs:element ref="article" minOccurs="0"
      maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="date" type="xs:string"/>
      </xs:complexType>
      </xs:element>
      <xs:element name="article">
      <xs:complexType>
      <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element ref="author" minOccurs="0"
      maxOccurs="unbounded"/>
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      <xs:element name="author" type="xs:string"/>
      </xs:schema>