This post contains attachments v20030325175816.zip 
Summary
This tutorial basically shows you the necessary steps on validating an XML file with a schema file with JAXP 1.2 running on J2SE 1.4.1
(The complete article in zip format is available for download - click the download zip icon.)
This tutorial basically shows you the necessary steps on validating an XML file with a schema file. It is adapted from the one found on java.sun.com which I thought was somehow not very explicit. The source code is clearer and has been tested with JAXP 1.2 running on J2SE 1.4.1.
This tutorial assumes that the reader has a fair knowledge of Java, XML and XML Schemas. If you aren't knowledgeable enough in both fields we suggest you read more about it here on TopXML for tutorials on XML and its related technologies. For Java tutorials, please go to http://java.sun.com or http://javaboutique.internet.com
For our example we would be using letter.xml as the source file and letter.xsd as the schema file.
First we declare a few contants we shall be using static final String JAXP_SCHEMA_LANGUAGE = http://java.sun.com/xml/jaxp/properties/schemaLanguage; static final String W3C_XML_SCHEMA = http://www.w3.org/2001/XMLSchema;
Create a new instance of the DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Configure DocumentBuilderFactory to generate a namespace-aware, validating parser that uses XML Schema factory.setNamespaceAware(true);
Set the property for schema validation to work factory.setValidating(true); try { Set a factory attribute specify the parser language to use factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); } catch (IllegalArgumentException iae) { Happens if the parser does not support JAXP 1.2 If you do have this error, please go to sun's XML download and download the latest version of Java API for XML Processing (JAXP 1.2 or greater). Follow the instructions on the readme file to create necessary jars using ant. Copy the jar files to your java library files folder which is usually located in your %JAVA_HOME%/jre/lib/ext. Incase you do have an xerces.jar file, make sure you delete it. }
Specify your schema file if you did not declare your schema in the XML document factory.setAttribute(JAXP_SCHEMA_SOURCE, YourSchemaFile.xsd);
Create a document builder DocumentBuilder builder = factory.newDocumentBuilder();
To be notified of validation errors in the XML document, add a custom error handler to the document builder MyDefaultHandler dh = new MyDefaultHandler(); builder.setErrorHandler(dh);
Parse the XML document with the document builder Document document = builder.parse(YourSourceFile.xml);
We get the result of parsing the document by checking the errorhandler's isValid property return (dh.isValid)?true:false; Creating a custom Error Handler
This is simply a empty class file that inherits (extends) everything from the sax helpers DefaultHandler class while overiding the Error handler methods of error, fatalError and warning that recieve SAXParseException error notifications. class MyDefaultHandler extends DefaultHandler { //Receive notification of a recoverable error. public void error(SAXParseException se) { ... get notification on errors .. } //Receive notification of a non-recoverable error. public voidfatalError(SAXParseException se) { ... get notification on fatal errors .. } //Receive notification of a warning. public void warning(SAXParseException se) { ... get notification on warnings .. } }
In order to check if there was an error we declare a public boolean variable isValid with its default value set to true. public booleanisValid = true; If any errors are notified we then set the value to false and check it value after the document is parsed. Source files:
- letter.xml
- letter.xsd
- ValidateXMLSchema1.java
(The complete article in zip format is available for download - click the download zip icon.)
For more information contact tanwani@dataml.net. Happy exemeling (X-M-El-ing)
| Further additional information | |
Updating comments...
Updating comments...
|