JUnit test for W3C Schemas

    public void testSchemaValidOutput() throws SAXException {
    
      File f = new File("filename.xml");
      InputSource in = new InputSource(new FileInputStream(f));

      XMLReader parser = XMLReaderFactory.createXMLReader(
        "org.apache.xerces.parsers.SAXParser"); 
      parser.setFeature("http://xml.org/sax/features/validation", true);
      parser.setFeature("http://apache.org/xml/features/validation/schema", true);
      parser.setProperty(
       "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", 
       "examples/plist.xsd");
      // also http://apache.org/xml/properties/schema/external-schemaLocation
      parser.setErrorHandler(new ErrorHandler() {

        public void warning(SAXParseException exception) throws SAXException {
          // skip
        }

        public void error(SAXParseException exception) throws SAXException {
          throw exception;      
        }

        public void fatalError(SAXParseException exception) throws SAXException {
          throw exception;       
        }
          
      });
      parser.parse(in);
      
    } 

Previous | Next | Top | Cafe con Leche

Copyright 2005 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified October 31, 2005