| home / programming / xml / parser / 1 | [previous][next] |
|
|
Rigorously testing preconditions is an important characteristic of robust, reliable software. Schemas make it very easy to define the preconditions for XML documents you parse and the postconditions for XML documents you write. Even if the document itself does not have a schema, you can write one and use it to test the documents before you operate on them. It is quite hard to attach a DTD to a document inside a program. Fortunately, however, most other schema languages are much more flexible about this.
For example, let's suppose you're in charge of a system at TV Guide that accepts schedule information from individual stations over the Web. Information about each show arrives as an XML document formatted as shown in Example 37-1.
Example 37-1 |An XML Instance Document Containing a Television Program Listing

Every day, around the clock, stations from all over the country send schedule updates like this one that you need to store in a local database. Some of these stations use software you sold them. Some of them hire interns to type the data into a password-protected form on your web site. Others use custom software they wrote themselves. There may even be a few hackers typing the information into text files using emacs and then telnetting to your web server on port 80, where they paste in the data. There are about a dozen different places where mistakes can creep in. Therefore, before you even begin to think about processing a submission, you want to verify that it's correct. In particular, you want to verify the following.
|
Example 37-2 |A W3C XML Schema for Television Program Listings

For simplicity, I'll assume this schema resides at the URL http://www. example.com/tvprogram.xsd in the examples that follow, but you can store it anywhere convenient.
There are several different ways to programmatically validate a document, depending on the schema language, the parser, and the API. Here I'll demonstrate two: Xerces-J using SAX properties and DOM Level 3 validation.
| home / programming / xml / parser / 1 | [previous][next] |
Created: March 27, 2003
Revised: October 25, 2003
URL: http://webreference.com/programming/xml/parser/1