JDK (Java Development Kit) Tutorials
∟XSD (XML Schema Definition) - XML Validation
∟Loading External XML Parsers with SAXParserFactory
This section provides a tutorial example on how to load external XML parsers using the SAXParserFactory.newInstance() method.
The Xerces-J package can also be loaded by the SAXParserFactory.newInstance() method. Here is my SAXValidator.java:
/** * SAXValidator.java * Copyright (c) 2002 by Dr. Herong Yang */ 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 SAXValidator { 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 the schema feature to the SAXParserFactory object.
Now if you run the program with:
java -cp . SAXValidator dictrionary_invalid_xsd.xml
You will get:
org.apache.crimson.jaxp.SAXParserFactoryImpl@1004901 org.xml.sax.SAXNotRecognizedException: Feature: http://apache.org/xml /features/validation/schema
Note that the crimson package was loaded from the J2SDK library, and it doesn't support XSD validation.
But if you run the program with:
java -cp .;\local\xerces-2_3_0\xercesImpl.jar SAXValidator dictrionary_invalid_xsd.xml
org.apache.xerces.jaxp.SAXParserFactoryImpl@f72617 org.apache.xerces.jaxp.SAXParserImpl@173831b ...
Note that the xerces package was loaded, and XSD validation was performed.
Last update: 2006.
Table of Contents
About This JDK Tutorial Book
Downloading and Installing JDK 1.3.1 on Windows
Downloading and Installing JDK 1.4.1 on Windows
Downloading and Installing JDK 1.5.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
Character Set Encoding Maps
Encoding Conversion Programs for Encoded Text Files
Socket Network Communication
Datagram Network Communication
DOM (Document Object Model) - API for XML Files
SAX (Simple API for XML)
DTD (Document Type Definition) - XML Validation
►XSD (XML Schema Definition) - XML Validation
XML XSD Schema Validation with Xerces
XML XSD Schema Validation Test Result
►Loading External XML Parsers with SAXParserFactory
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
SSL Client Authentication
HTTPS (Hypertext Transfer Protocol Secure)
References
PDF Printing Version