Abbey Workshop

Schema Validation with JAXP

This tip is an example Java program for validating an XML file using an W3C XML Schema. This example uses Sun JAXP 1.2 API and JDK 1.4.0. JAXP 1.2 uses Apache Xerces as its underlying XML parser.

Note: This tip is current at the time of this writing. However, the XML standards are evolving daily and the parsing code may change anytime a new standard is introduced.

To make this example work, you must download and install the latest JAX Pack from Sun at http://java.sun.com/xml.

Reviewing the Code

SimpleSchema.java

Download source for: SimpleSchema.java

Note:The following are sample XML files you can use to test the class.

View: todo.xml | todo.xsd | todoBad.xml

Download: todo.xml | todo.xsd | todoBad.xml

There are a couple of differences you will see right off the bat from a standard XML parser. Lines 15-18 define constants for parser features we want to use in our program. The JAXP_SCHEMA_LANGUAGE constant is used to define the language the schema is written in. The W3C_XML_SCHEMA constant identifies W3C XML Schema as the language we will be using to validate. The JAXP_SCHEMA_SOURCE constant is used when you which to specify a schema document different from the one specified in the XML document. We are not using this constant in this example.

Next, look at the SimpleSchema() constructor (line 21) for the key configuration steps used in creating our parsing object.

  1. Line 23-25. Create a SAXParserFactory to set the features for this parser. In this case, validation is enabled as well as namespace support. When using schemas, you must enable namespace support
  2. Line 32. Create a SAXParser object.
  3. Line 35. Set the Schema Language.
  4. Line 38. Create an XMLReader object to parse the document
  5. Line 40-44. Create and set the ErrorHandler and ContentHandler. In this example, the ContentHander is unecessary since no processing of elements or attributes is being done in the program. However, normally you would set the ContentHandler so I left the code in. The ErrorHandler is required to see error messages generated during parsing.

The parser is invoked on line 55. This method call parses the file and calls the ErrorHandler whenever an error is encountered.

The Error Handling Code

Line 85-107. This is the error handling code. It is pretty simple really. Create warning(), error(), and fatalError() methods as shown. The appropriate method is called depending on the error generated. Warnings and errors will not stop parsing, however, fatal errors will.

Google
  Web abbeyworkshop.com   

Copyright © Abbey Workshop 2006

Music Only Search
The Black MP3 Album
The MP3 Album Lyrics
Wprld Music Plus
Mp3 Songs Blog
Music Search Store
First Music Search
Mega MP3 Blog
E MP3 BLog
My MP3 Diary
Fast MP3 Finder
My Blog MP3
Mp3 Songs Mart
My MP3 Songs Album
Top Music Group
Mp3 Tune Records
Song Album Guide
Music DataDite
MP3 Buying Guide
Top MP3 Downloads
MP3 Tune Festival
FAST MP3 Downloads
MP3 Mart Online
Upper MP3
Premium MP3 Site
Music Data online
Fine MP3 Audio
Cheap Music Albums
Fine mp3 downloads
World MP3 Music Records
Mp3 Music Data Direct
The Fine Mp3
MP3 Files World
Home Music Store
DatMP3
Fat MP3 Audio
MP3 ZigZag
MP3 Songs Direct
MP3 Tune World
E MP3 Log
Boom MP3 Music
Speedy MP3
Top 20 MP3 music
Deep MP3 World
Deep MP3 Download
Best-20 MP3
Top Twenty MP3
Deep MP3
Big MP3 Guide
Deep MP3 Audio
Mp3 R.O.B.
MP3-Pop Online
Greate-20-MP3
MP3 Appear
3T-MP3