Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Sequential XML Processing With The XMLReader Object (part 2)
Validate your XML against DTDs and XML Schemas with the XmlValidatingReader class.

| Returning To The Library |

I'll explain how the XMLValidatingReader works by again referring to the sample XML instance created in the first part of this article. In case you don't remember what it looked like, here it is again:


<?xml version='1.0'?>
<library xsi:noNamespaceSchemaLocation="library.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<book id="MFRE001">
<title>XML and PHP</title>
<author>Vikram Vaswani</author>
<description>Learn to manage your XML data with PHP</description>
<price currency="USD">24.95</price>
</book>
<book id="MFRE002">
<title>MySQL - The Complete Reference</title>
<author>Vikram Vaswani</author>
<description>Learn everything about this open source database</description>
<price currency="USD">45.95</price>
<stock>1000</stock>
</book>
</library>


The only major difference in this version of the XML file is the introduction of the "xsi:noNamespaceSchemaLocation" attribute. For the uninformed, this holds the location of the Schema against which this document is to be validated.

And here's the XML Schema against which the XML document listed above was originally built:


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="library" type="LibraryType"/>
<xsd:complexType name="LibraryType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book"  type="BookType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" />
<xsd:element name="author" type="xsd:string" />
<xsd:element name="description" type="xsd:string" />
<xsd:element name="price">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="currency" type="xsd:string" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" />
</xsd:complexType>
</xsd:schema>


Now for the glue that binds them. Consider the following ASP.NET code, which validates the XML document instance against the XML Schema above:


<%@ Page Language="C#" Debug="true" %>
<%@ Import namespace="System.Xml"%>
<%@ Import namespace="System.Xml.Schema"%>
<html>
<head>
<script runat="server">

Boolean blnValidationSuccess = true;

void Page_Load()  {

// define variables
string strXmlFile = "http://localhost:2121/xmlpull/library.xml";

// initialize the XML readers 
XmlTextReader objXmlTxtRdr = new XmlTextReader(strXmlFile);
XmlValidatingReader objXmlValRdr = new XmlValidatingReader(objXmlTxtRdr);

// set the validation type
objXmlValRdr.ValidationType = ValidationType.Schema;

// set the validation event handler
objXmlValRdr.ValidationEventHandler += new ValidationEventHandler (ValidationMonitor);

// show some status messages
output.Text = "Validating file: <b>" + strXmlFile.ToString() + "</b>";

// read XML data
while (objXmlValRdr.Read()){}

output.Text += "<br />Validation <b>" + (blnValidationSuccess == true ? "successful" : "failed") + ".</b>";

objXmlValRdr.Close();
objXmlTxtRdr.Close();

}

// display validation errors
void ValidationMonitor (object sender, ValidationEventArgs args)
{
  blnValidationSuccess = false;
  output.Text += "<br />Validation Error: <i>" + args.Message + "</i>";
}

</script>
</head>
<body>
<asp:label id="output" runat="server"/>
</body>
</html>


If you were to test this code using the "library.xml" file shown above, the XML document instance should pass the validation tests with flying colours:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Validating file: http://localhost:2121/xmlpull/library.xml
Validation successful.
').'
'

But look what happens if you add a new, unwanted element to the document instance:


<?xml version='1.0'?>
<library>
<book id="MFRE001">
<title>XML and PHP</title>
<author>Vikram Vaswani</author>
<description>Learn to manage your XML data with PHP</description>
<price currency="USD">24.95</price>
</book>
<book id="MFRE002">
<title>MySQL - The Complete Reference</title>
<author>Vikram Vaswani</author>
<description>Learn everything about this open source database</description>
<price currency="USD">45.95</price>
<inventory>12</inventory>
</book>
</library>


The XML Schema definition does not allow the XML author to add this new  <inventory> element. That's why you'll see the following output when you reload the example in the browser:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Validating file: http://localhost:2121/xmlpull/library.xml
Validation Error: The element 'book' has invalid child element 'inventory'. An error occurred at http://localhost:2121/xmlpull/library.xml, (15, 4).
Validation Error: The 'inventory' element is not declared. An error occurred at http://localhost:2121/xmlpull/library.xml, (15, 4).
Validation failed.
').'
'


Notice that the error message explicitly highlights the rogue <inventory> element in the XML file.

Now, let's take a closer look at how this code works. It all starts with the definition of a flag variable to track the validation process.


<%

Boolean blnValidationSuccess = true;

%>


This is followed by the definition of the object required for our example. Here, I need to first initialize a plain-vanilla XmlTextReader object, and then pass this object as a parameter to the new XmlValidatingReader object, as shown below:


<%

// initialize the XML readers 
XmlTextReader objXmlTxtRdr = new XmlTextReader(strXmlFile);
XmlValidatingReader objXmlValRdr = new XmlValidatingReader(objXmlTxtRdr);

%>


Next, I have defined the mechanism to use when validating the XML - in this case, an XML Schema. This is done via the "ValidationType" property of the XmlValidatingReader object:


<%

// set the validation type
objXmlValRdr.ValidationType = ValidationType.Schema;

%>


You can set the "ValidationType" property of the XmlValidatingReader object to any one of the following:

"ValidationType.None" - no validation is required

"ValidationType.Auto" - search for a file automatically; if available, carry out validation

"ValidationType.DTD" - perform validation using a DTD

"ValidationType.XDR" - perform validation using a XDR

"ValidationType.Schema" - perform validation using an XML Schema

While the validator is checking the XML document against the Schema, it generates an event if it encounters an error. Therefore, it's a good idea to define an event handler to trap this event and take appropriate action when it occurs. In this example, I've defined an event handler function named ValidationMonitor(), and associated it with the object via its "ValidationEventHandler property":


<%

// set the validation event handler
objXmlValRdr.ValidationEventHandler += new ValidationEventHandler (ValidationMonitor);

void ValidationMonitor (object sender, ValidationEventArgs args)
{
  blnValidationSuccess = false;
  output.Text += "<br />Validation Error: <i>" + args.Message + "</i>";
}

%>


Notice how the "Message" property of the object is used to display a user-friendly error message in the browser.

Finally, assuming no errors in validation, you can iterate over the document and process the XML inside it with the Read() method I showed you in the previous article. Here, the Read() loop is an empty block because I didn't really want to process the data in the file, just validate it to show you how it was done.


<%

// read XML data
while (objXmlValRdr.Read()) {

}

output.Text += "<br />Validation <b>" + (blnValidationSuccess == true ? "successful" : "failed") + ".</b>";

%>


The example closes with a check on the "blnValidationSuccess" variable, displaying the appropriate outcome of the validation process to the user in the browser.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback