W3C home > Mailing lists > Public > xmlschema-dev@w3.org > June 2004

Schema API

From: Lachlan Aldred <l.aldred@qut.edu.au>
Date: Mon, 21 Jun 2004 12:46:50 +1000
Message-Id: <>
To: xmlschema-dev@w3.org

Does anyone know a good XML Schema API?

I had a quick look at W3 XML Schema API Web pages, also XERCES/PSVI to see 
if it could offer the sort of features I was after, and I have some questions:

How would I load a just a schema (no instance) to create some schema 
objects in Java?

How do I serialize parts of the schema (for example one type def) to create 
a self contained schema declaration?  This is problematic when a type 
definition refers to another root level type/element definition elsewhere 
in the document, or in another document.

If I have a schema object how can I validate instances against it?

Out of interest I developed a program in Java that uses Xerces to validate 
a schema against the schema specification and it gives line number reports 
etc.  It's a bit of a hack.  It takes schemas as strings, which suits my 
purposes, and if they are valid returns an empty string.    Is there a 
cleaner way of doing that too?

import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.jdom.input.SAXBuilder;

import java.io.StringReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;

public class SchemaValidator extends DefaultHandler {
     StringBuffer _errorsString = new StringBuffer("");
     private static SchemaValidator _myInstance;
     private SAXBuilder _builder;
     private File _tempSchema;

      * If the string is of 0 length then you know it has passed the test.
      * @return the validation failures as a string.
     public String validateSchema(String schema) {
         File userDir = new File(System.getProperty("user.dir"));
         _tempSchema = new File(userDir, "_tempSchema.xsd");
         try {
             FileWriter writer = new FileWriter(_tempSchema);
         } catch (IOException e) {
         String instance =
                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
         String result = checkSchema(new InputSource(new 
         return result;

     public static SchemaValidator getInstance() {
         if (_myInstance == null) {
             _myInstance = new SchemaValidator();
         return _myInstance;

     private SchemaValidator() {
         _builder = new SAXBuilder();

     public void warning(SAXParseException ex) {
         addMessage(ex, "Warning");

     public void error(SAXParseException ex) {
         addMessage(ex, "Invalid");

     public void fatalError(SAXParseException ex) throws SAXException {
         addMessage(ex, "Error");

     private void addMessage(SAXParseException e, String errType) {
         String lineNum = getLineNumber(e);
         if (lineNum != null) {
             _errorsString.append(errType + "#" + lineNum + "# " + 
e.getMessage() + '\n');

     private String getLineNumber(SAXParseException e) {
         String fileURL = e.getSystemId();
         if (fileURL != null) {
                     "[ln: " + e.getLineNumber() + " col: " + 
e.getColumnNumber() + "]";
         return null;

     private String checkSchema(InputSource input) {
         _errorsString.delete(0, _errorsString.length());
         try {
             XMLReader parser = setUpChecker();
         } catch (SAXParseException e) {
         } catch (Exception e) {
         return _errorsString.toString();

     private XMLReader setUpChecker() throws SAXException {
         XMLReader parser = 
         parser.setFeature("http://xml.org/sax/features/validation", true);
         try {
             URL schemaURL = _tempSchema.toURL();
                 "" + schemaURL);
         } catch (MalformedURLException e) {
         return parser;



Lachlan Aldred
Queensland University of Technology
Received on Sunday, 20 June 2004 22:46:29 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:56:05 UTC