JDK Tutorials - Herong's Tutorial Examples - Version 6.02, by Dr. Herong Yang
XSD Validation Failed with SAXParserFactory
This section provides a tutorial example showing the SAXParserFactory class failed to perform validation with XSD schema file specified in XML documents.
I think we can also use SAXParser class to validate an XML document against an XSD schema. Let me try with this sample program SAXValidator2.java:
/* SAXValidatorXSD.java - Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ import java.io.File; import java.io.IOException; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParser; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; class SAXValidatorXSD { public static void main(String[] args) { String schemaFeature = "http://apache.org/xml/features/validation/schema"; try { File x = new File(args[0]); SAXParserFactory f = SAXParserFactory.newInstance(); System.out.println(f.toString()); f.setValidating(true); f.setFeature(schemaFeature,true); SAXParser p = f.newSAXParser(); System.out.println(p.toString()); DefaultHandler h = new MyErrorHandler(); p.parse(x,h); } catch (ParserConfigurationException e) { System.out.println(e.toString()); } catch (SAXException e) { System.out.println(e.toString()); } catch (IOException e) { System.out.println(e.toString()); } } private static class MyErrorHandler extends DefaultHandler { public void warning(SAXParseException e) throws SAXException { System.out.println("Warning: "); printInfo(e); } public void error(SAXParseException e) throws SAXException { System.out.println("Error: "); printInfo(e); } public void fatalError(SAXParseException e) throws SAXException { System.out.println("Fattal error: "); printInfo(e); } private void printInfo(SAXParseException e) { System.out.println(" Public ID: "+e.getPublicId()); System.out.println(" System ID: "+e.getSystemId()); System.out.println(" Line number: "+e.getLineNumber()); System.out.println(" Column number: "+e.getColumnNumber()); System.out.println(" Message: "+e.getMessage()); } } }
Note that I set the schema feature to the SAXParserFactory object.
Now run the program with JDK 1.8:
>java SAXValidator2 dictrionary_invalid_xsd.xml com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl@1db9742 com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl@106d69c Error: Public ID: null System ID: file:/C:/herong/dictionary_invalid_xsd.xml Line number: 3 Column number: 49 Message: cvc-elt.1: Cannot find the declaration of element 'dictionary'. Fattal error: Public ID: null System ID: file:/C:/herong/dictionary_invalid_xsd.xml Line number: 14 Column number: 10 Message: The content of elements must consist of well-formed character data or markup. org.xml.sax.SAXParseException; systemId: file:/C:/herong/dictionary_invalid_xsd.xml; lineNumber: 14; columnNumber: 10; The content of elements must consist of well-formed character data or markup.
Unfortunately, the test program failed to load the XSD schema file "dictionary.xsd", which is specified in the attribute xsi:noNamespaceSchemaLocation="dictionary.xsd" in the XML document. This is why we are getting first error "Cannot find the declaration of element 'dictionary'".
See the next tutorial for another way of validating XML document against XSD schema with SAX parser.
Last update: 2014.
Table of Contents
Downloading and Installing JDK 1.8.0 on Windows
Downloading and Installing JDK 1.7.0 on Windows
Downloading and Installing JDK 1.6.2 on Windows
Date, Time and Calendar Classes
Date and Time Object and String Conversion
Number Object and Numeric String Conversion
Locales, Localization Methods and Resource Bundles
Calling and Importing Classes Defined in Unnamed Packages
HashSet, Vector, HashMap and Collection Classes
Character Set Encoding Classes and Methods
Encoding Conversion Programs for Encoded Text Files
Datagram Network Communication
DOM (Document Object Model) - API for XML Files
DTD (Document Type Definition) - XML Validation
►XSD (XML Schema Definition) - XML Validation
XML XSD Schema Validation Test Result
XML XSD Schema Validation with Xerces
►XSD Validation Failed with SAXParserFactory
XSD Schema Validator on XML SAX Object
XSL (Extensible Stylesheet Language)
Message Digest Algorithm Implementations in JDK
Private key and Public Key Pair Generation
PKCS#8/X.509 Private/Public Encoding Standards
Digital Signature Algorithm and Sample Program
"keytool" Commands and "keystore" Files
KeyStore and Certificate Classes
Secret Key Generation and Management
Cipher - Secret Key Encryption and Decryption
The SSL (Secure Socket Layer) Protocol
SSL Socket Communication Testing Programs