XML Processing With The XMLReader Object, Part 2 - Returning to the Library
(Page 2 of 6 )
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 colors:
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:
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.
Next: To DTD or Not to DTD >>
More XML Articles
More By Harish Kamath (c) Melonfire