User Tools

Site Tools


technology:java:xml_schema_validator

This is an old revision of the document!


XML Schema Validator

This class was designed as a plug-in for the Edit Plus text editor. It uses the SAXParser object from the Apache Xerces project. Features like schema and namespace validation can be turned on/off by commenting/uncommenting the appropriate lines in the code example

Note:

  • add a package statement and install to appropriate location on your machine
  • new versions of the SAXParser may depricate this code or make it need updates
import java.io.*;
import javax.swing.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
 
class validator
{
    public static void main(String[] args)
    {
        if (args.length == 0)
        {
            JOptionPane.showMessageDialog(null, "No Filename passed in.");
            System.exit(1);
        }
    try{
        //SAX
        Class.forName("org.apache.xerces.parsers.SAXParser");
        XMLReader xr = new org.apache.xerces.parsers.SAXParser();
//        xr.setFeature("http://xml.org/sax/features/validation", true);
//        xr.setFeature("http://apache.org/xml/features/validation/schema", true);
        xr.setFeature("http://xml.org/sax/features/namespaces", true);
        xr.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
 
        InputStream in  = new FileInputStream(new File(args[0]));
        xr.setErrorHandler(new validatorErrorHandler());
        xr.parse(new InputSource(in));
 
        //JOptionPane.showMessageDialog(null, "Document validates OK.");
        System.out.println("Document validates OK.");
        System.exit(1);
    }
    catch (SAXException se) {
        //JOptionPane.showMessageDialog(null, "SAX Exception:  "+se.getMessage());
        System.out.println("SAX Exception:  "+se.getMessage());
        System.exit(1);
    }
    catch (IOException ioe) {
        //JOptionPane.showMessageDialog(null, "IO Exception:  "+ioe.getMessage());
        System.out.println("IO Exception:  "+ioe.getMessage());
        System.exit(1);
    }
    catch (ClassNotFoundException cnfe) {
        //JOptionPane.showMessageDialog(null, "Parser Not Found");
        System.out.println("Parser Not Found");
        System.exit(1);
    }
}
 
}
/home/cfreyer/public_html/data/attic/technology/java/xml_schema_validator.1238596596.txt.gz · Last modified: 2009/04/01 10:36 by Chris Freyer