ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
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 ..


1 to 1 of 1
  1. Handler exception
    2006-06-14 07:23:45  Deepak Vohra | [View]

    • Handler exception
      2006-06-14 21:44:49  nnc [View]

1 to 1 of 1