introduction to xml - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · web...

32
CPAN 560 XML Lecture #6:DTD The Document Type Definitions is a set of rules that define the structure and syntax of an XML document. Therefore, DTD provides a mechanism for validating XML documents against the document vocabulary rules. The XML document’s vocabulary is a description of the data contained in this document. Any number of XML documents that share the same XML vocabulary is known as a document type, and each one of them is called a document instance. Valid XML documents are well-formed XML documents that meet the syntax, rules, and structure that are defined in a DTD . With DTD , each XML document can carry its own format , and verify its data. DTD can be associated internally within the XML document, referenced externally, or both ways. The internal declarations have more priority than the external declarations. DTD declarations are delimited with < and >, and each declaration is preceded by exclamation. If we associated an XML document with an external DTD, we have to set standalone attribute of XML declaration to no. The purpose of DTD is to define the legal building block of an XML document. From the DTD point of view, any XML document is composite of the following building blocks: DOCTYPE declaration. ELEMENTS declaration. ATTLIST declaration. 1

Upload: others

Post on 06-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

CPAN 560 XML

Lecture #6:DTD

The Document Type Definitions is a set of rules that define the structure and syntax of an XML document. Therefore, DTD provides a mechanism for validating XML documents against the document vocabulary rules.

The XML document’s vocabulary is a description of the data contained in this document. Any number of XML documents that share the same XML vocabulary is known as a document type, and each one of them is called a document instance.

Valid XML documents are well-formed XML documents that meet the syntax, rules, and structure that are defined in a DTD . With DTD , each XML document can carry its own format , and verify its data.

DTD can be associated internally within the XML document, referenced externally, or both ways. The internal declarations have more priority than the external declarations.

DTD declarations are delimited with < and >, and each declaration is preceded by exclamation. If we associated an XML document with an external DTD, we have to set standalone attribute of XML declaration to no.

The purpose of DTD is to define the legal building block of an XML document.

From the DTD point of view, any XML document is composite of the following building blocks:

DOCTYPE declaration. ELEMENTS declaration. ATTLIST declaration. ENTITY declaration. NOTATION declaration.

We will discuss each building block in a separate section.

 

DOCTYPE Declaration

This optional declaration is used to associate an XML document with a DTD, and it can appears only once in an XML document. DOCTYPE must follow the

1

Page 2: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

document’s XML declaration, but it may be preceded with comments or processing instructions. The syntax of internal DTD is:

 <!DOCTYPE root-element reference_to_external_DTD [ internal element-declarations]>

Even though one DOCTYPE declaration is allowed with any XML document, we can see that it allow us to associate an XML document with DTD externally , internally , or have both internally and externally declarations.

To associate an XML document with a DTD document externally we have to define the source and location of the DTD document.

The source can be either SYSTEM or PUBLIC. When SYSTEM is used, the location of the DTD should be provided using URI. For example:

<!DOCTYPE employee SYSTEM  "employee.dtd" >

This means that the XML document employee is associated with a DTD called employee.dtd that is located in the same folder of the XML document.

We can store the DTD document anywhere on our local physical driver. For example:

<!DOCTYPE employee SYSTEM  "file:///c:/employee.dtd" >

The DTD document may be located somewhere on the Internet. For example:

<!DOCTYPE student SYSTEM  "http://www.mysite.com/student.dtd" >

PUBLIC reference a DTD by defining an entry in a Catalogue in a format known as Formal Public Identifier. FPI has the following syntax:

Registration indicator //owner//Class Name and Description//Language// Version

When the FPI is registered, then the registration indicator should be represented by the symbol –. When it is not registered the registration indicator should be presented by the symbol +.

2

Page 3: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

An optional URL reference may be added following the FPI to be used when the XML validating tool cannot locate the public FPI. For example:

<!DOCTYPE booksPUBLIC "-//mysite//DTD document //EN" "books.dtd" >

Elements Declaration

Declares an XML element in one of two following formats:

<! ELEMENT element_name category>

<! ELEMENT element_name content_model >

category, and content_model describe the content of this element. Elements have five categories of contents:

  Content category

Description

ANY The element may contain any combination of well-formed XML data. This includes character data, other elements, comments, processing Instructions, and CDATA section.

EMPTY The element cannot contain anything except attributes.Element The element can contain only child elements.Mixed The element may contain text, child elements, or both. PCDATA The element may contain text only that will be parsed by

the parser.

The content categories ANY, and EMTY use the first form of the ELEMENT declaration. The content categories: element, mixed, and PCDATA are used to restrict the element content and thus they use the second form of the ELEMENT declaration. They may also use the Cardinality Operators.

The Cardinality Operators define how many times the child element can appear.

Cardinality Operator

Description

None The child element can appear only once.? The child element can appear zero or one time.* The child element can appear zero or more times.+ The child element can appear one or more times.

When we list child elements we can use the following list operators:

3

Page 4: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

List Operator Description, Used to indicate that the child elements should appear

in the specified sequence. | Used to indicate that only one of many child element

is permitted.

Below are some examples for declaring elements:

Element Declaration Description<!ELEMENT memo AMPTY>

Declare an element called memo that has no content.

<!ELEMENT emp (id,name,address?,project*)>

Declare an element called emp that has the following child elements:

id should appear only once.

name should appear only once.

address can appear zero or one time

project can appear zero or more times .

These child elements must appear in the same order in the declaration. Also we have to provide a n element declaration for each of them.

<!ELEMENT person (name|(fname, lname))>

Declare an element called person that can have either name or fname and lname as child element(s). We also have to provide separate element declaration for each of these child elements as shown in the following example..

<!ELEMENT address (#PCDTA)>

Declare an element called address that can have parsed character content.

 

 Attributes Declaration

The ATTLIST declaration is used to define the attributes that can be associated with a specific element and their types. It has the following format:

4

Page 5: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

<!ATTLIST element_name attribute_name attrbute_type attribute_Default default_value>

attribute_type defines how the parser will treat the character data contained in the attribute value. It can be any of the following types:Attribute Type

Description

CDATA Text (Character data). This is the default type.(val1|val2|..) The value of the attribute must be one from an enumerated

list ID The value of the attribute is a unique identifier.IDREF The value of the attribute is the id of another element.IDREFS The value of the attribute is a list of other ids.NMTOKEN The value of the attribute is a valid XML name.NMTOKENS The value of the attribute is a list of valid XML names.ENTITY The value of the attribute is the name of predefined

ENTITY.ENTITIES The value of the attribute is a list of ENTITY names

delimited by white space.NOTATION The value of the attribute is a notation type that is explicitly

declared somewhere in the DTD. The attrribute_default determine whether the present of the attribute is required or not. It can be: #REQUIRED, #IMPLIED, or #FIXED.The default_value provide a default value for the attribute if at was not present.Below are some examples of attributes declarations:

Attribute Declaration Description<!ATTLIST student st_no ID #REQUIRED >

Declare an attribute called st_no that is associated with an element called student. The attribute values should be unique and are required.

<! ATTLIST name title (Mr|Mss|Mrs) #IMPLIED>

Declare an optional attribute called title that is associated with an element called name. The value of this attribute should be one of the provided values: Mr, Mss, or Mrs.

<! ATTLIST semester no CDATA "no1">

Declare an attribute called no that is associated with an element called semester. The value of this attribute is a character data. If no value is provided, then the default value “no1” will be used as default value for this

5

Page 6: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

attribute. 

Entities Declaration

Entities are variables used to define common text or special characters either in the XML document or its associated DTD document. When an XML document is parsed, entities are replaced by their references. There are four main kinds of entities:

Built-in Entities

The following five build-in entities are used within an XML document to reference their corresponding characters:

Entity Reference Description

&lt; Reference to the character <

&gt; Reference to the character >

&amp; Reference to the character &

&quot; Reference to the quote character.

&apos; Reference to the apostrophe character.  For example

<Book publisher=”John &amp; Wiley Publishers”>

 Or

<Book><publisher> John &amp; Wiley Publisher</publisher></book> 

Character Entities

 Character entities are also used within an XML document to reference special characters. The flowing table list the references to some of the special  

Entity Reference Description&#163; Reference to the character £&#169; Reference to the character ©

&#174; Reference to the character ®.

&#177; Reference to the character ±

6

Page 7: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

&#199; Reference to the character Ç.

 For example:

<Book><note> &#169; 2003</note></book> 

General Entities

General entities are used to reference a section of text. They should be declared in the DTD before we can use them in an XML document. For example we may declare the following general entity in a DTD document:<!ENTITY comp “ Universal IT Solutions”>Then in the XML document we can have the following segment:<company> &comp; <company>or the following XML document segment:<company name=”&comp;” />

Parameter Entities

Parameter Entities are used in DTD to reference a section of text. We also need to declare parameter entities in the DTD before we can use them. For example assume that we have the following DTD document segment:

<!ENTITY % CUR "'CAD'"><!ATTLIST Pricecurrency CDATA %CUR; >  

 

Notation Declaration

Notation Declaration is used to include a reference to an unparsed ( non text – based) data like images, word documents, and spreadsheets in our XML document. Because the XML parser cannot parse such data, Notation declaration associates types of external resources with external helper applications that can handle them. The XML parser responsibility is to provide the external helper application with such data and its up to the application to determine how the data will be processed. For example the following XML document shows how to use Notation declaration:

<?xml version="1.0" ?><!DOCTYPE employees SYSTEM "emp.dtd">

7

Page 8: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

<employees><emp image="empImage1"><id>1</id><name>John Smith</name></emp><emp image="empImage2"><id>2</id><name>Dona Brown</name></emp></employees> 

And following is the associated DTD file (emp.dtd):

<!NOTATION bmp SYSTEM "explorer.exe"><!ENTITY empImage1 SYSTEM "image1.bmp" NDATA bmp><!ENTITY empImage2 SYSTEM "image2.bmp" NDATA bmp><!ELEMENT employees (emp+)><!ELEMENT emp (id,name)><!ELEMENT id (#PCDATA)><!ELEMENT name (#PCDATA)><!ATTLIST emp image ENTITIES #REQUIRED>

 

DTD tools 

Internet Explorer Tools for Validating and Viewing XSLT Output (iexmltls.exe):

 Microsoft has an XML tool that can be used to validate an XML document, and view XSLT output. You can download the executable file for this tool from the web site http://microsoft.com/downloads/ 

 To install this tool, you need to run the downloaded file, navigate through the folder C:\IEXMLTLS to find the files: msxmlval.inf and msxmlvw.inf. Right click each and select install from the popup menu as shown below:

8

Page 9: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

 The two tools will be added to the popup menu of the browser. To check these tools, open Internet Explorer and right click anywhere inside the browser window. You should see these tools added to the popup menu.

 

 

Examples

  

Ex1:student.xml

In this example we will see how to associate a DTD with and XML document internally.

<?xml version="1.0"?><!DOCTYPE students [<!ELEMENT students (student+)><!ELEMENT student (name,address+,semester)><!ELEMENT name (firstName,lastName)>

9

Page 10: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

<!ELEMENT firstName (#PCDATA)><!ELEMENT lastName (#PCDATA)><!ELEMENT address (#PCDATA)><!ELEMENT semester EMPTY><!ATTLIST student id ID #REQUIRED ><!ATTLIST semester no CDATA "no1" > ]><students><student id="no1"><name><firstName> John</firstName><lastName>Smith</lastName></name><address></address><semester no="2"/></student><student id="no2"><name><firstName>Robert</firstName><lastName>Smart</lastName></name><address></address><semester /></student></students>

According to the DTD, we have the following rules: The root element is called students. It has one child element called

student that can appear one or more times. student should have the following child elements in sequence:

o name This element should have the following child elements in sequence: 

o firstName This element should have parsed character data, and can appear only once. 

o lastName This element should have parsed character data, and can appear only once.

o address This element should have parsed character data, and can appear zero or one time.

o semester This element has no content, and should appear only once. semester should have an attribute called no. The values of this attribute are character data, and the default value is “no1”.

 student should also have a required attribute called id. The values of this attribute should be unique.

10

Page 11: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

To validate this XML document, open it in Internet Explorer, and right click anywhere inside the browser window as shown below:

 Select Validate XML from the popup menu. A dialog will appear indicating whether the document is valid or not. The source of error will be reported if this dialog if the document was not valid. Ex2: Books.xml

 This example will associate the XML document Books.xml with the external DTD Books.dtd.

 The file Books.dtd has the following content:

<!-- element declaration --><!ELEMENT Books (Book*)><!ELEMENT Book (Title,ISBN,Author*,Publisher?,Edition?,Price?)><!ELEMENT Title (#PCDATA)><!ELEMENT ISBN (#PCDATA)><!ELEMENT Author (#PCDATA)>

11

Page 12: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

<!ELEMENT Publisher (Name,Email?)><!ELEMENT Name (#PCDATA)><!ELEMENT Email (#PCDATA)><!ELEMENT Edition (#PCDATA)><!ELEMENT Price (#PCDATA)><!-- parameter entity. Used in DTD only --> <!ENTITY % CUR "'CAD'"><!--external general entities.Used in XML --><!ENTITY theName "Muthana Abdullah" ><!ENTITY copy "&#169;" ><!-- attributes for elements --><!ATTLIST Books manager CDATA #IMPLIED><!ATTLIST BookBookNo ID #REQUIREDcategory (Programming|Systems|DataBase|Internet) #IMPLIED ><!ATTLIST Authorid CDATA #REQUIREDposition CDATA #IMPLIED><!ATTLIST Pricecurrency CDATA %CUR;>

The following is a description of the rules defined inBooks.dtd :

The root element is called Books, and has one child element called Book. Book can appear zero or more times. The element Books may have an attribute called manager. The value of this attribute is character data.

Book element has the following child elements that must appear in sequence:

Title The content of this element is parsed character data, and can appear once.

ISBN The content of this element is parsed character data, and can appear once.

Author The content of this element is parsed character data, and can zero or more times. If this element is present in the XML document, it should have the following two attributes:

1.       id This attribute is required and has character data values.

2.       position This attribute is optional and has character data values

12

Page 13: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

Publisher can appear zero or one time. This element has the following child elements:o Name The content of this element is parsed character

data, and can appear once.o Email The content of this element is parsed character

data, and can appear zero or one time. Edition The content of this element is parsed character data,

and can appear zero or one time. Price The content of this element is parsed character data, and

can appear zero or one time. If this element is present in the XML document, then it should have an attribute called currency. The values of this attribute is character data, and the default value is “CAD”.

Book should have the following two attributes:

1.       BookNo This attribute is required. The values of this attribute should be unique.

2.       Category This attribute is optional. The value of this attribute should be one of the values: Programming, Systems, DataBase, or Internet.

 Notice also that we are declaring many entities that are either used within the DTD document , or within the XML document.

 And here is the XML file Books.xml that adhere the rules defined in Books.dtd:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE Books SYSTEM "books.dtd"><Books manager="&theName; &copy; 2002"><Book BookNo="no1"><Title>XML</Title><ISBN>123-123-123</ISBN><Author id="1">Tom Prince</Author><Author id="2">Smith wayne</Author><Publisher><Name>Home publishing</Name></Publisher><Edition>2nd,2002</Edition><Price currency="USA">55</Price></Book><Book BookNo="no2" category="Programming">

13

Page 14: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

<Title>XML</Title><ISBN>123-123-123</ISBN><Author id="1">Tom Prince</Author><Author id="2">Smith wayne</Author><Publisher><Name>Home publishing</Name><Email>[email protected]</Email></Publisher><Edition>2nd,2002</Edition><Price>55</Price></Book></Books>

Ex3:DOMValidator.java 

In this example we will create a Java GUI application that validates an XML document against a DTD using DOM implementation: 

 

import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.io.*;import javax.xml.parsers.*;import org.w3c.dom.*;import org.xml.sax.*;import org.xml.sax.helpers.*;public class DOMValidator extends JFrame implements ActionListener{

private File xmlFile;private StringWriter sr;private JMenuBar bar;private JMenu start;private JMenuItem xmlItem,validateItem,exitItem;private JTextArea out;public DOMValidator(){super ("DTD Validation using DOM ");Container c=getContentPane();c.setLayout(new BorderLayout());

14

Page 15: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

// text area to display the resultsout =new JTextArea(10,20);out.setEnabled(false);

// menu bar for optionsbar=new JMenuBar();start=new JMenu("Options");

xmlItem=new JMenuItem("Load XML file");xmlItem.setMnemonic('L');xmlItem.addActionListener(this);

validateItem=new JMenuItem("Validate");validateItem.setMnemonic('V');validateItem.addActionListener(this);

exitItem=new JMenuItem("Exit");exitItem.setMnemonic('E');exitItem.addActionListener(this);

setJMenuBar(bar);

start.add(xmlItem);

start.add(validateItem);start.add(exitItem);bar.add(start);

c.add(new JScrollPane(out),BorderLayout.CENTER);

setSize(400,400);setVisible(true);}

// events handlerpublic void actionPerformed(ActionEvent e){

if (e.getSource()==xmlItem){// get the XML fileJFileChooser fc=new JFileChooser();fc.setFileSelectionMode(JFileChooser.FILES_ONLY);int result=fc.showOpenDialog(this);

15

Page 16: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

if(result==JFileChooser.CANCEL_OPTION)return;

xmlFile=fc.getSelectedFile();if(xmlFile==null|| xmlFile.getName().equals(""))JOptionPane.showMessageDialog(this,"Invalid file name","Invalid file name",JOptionPane.ERROR_MESSAGE);

}else if (e.getSource()==validateItem){validateDocument();

}

else if (e.getSource()==exitItem){System.exit(0);}}public void validateDocument(){//create a new XML document using XML DOMDocument doc=null;try{DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();f.setValidating(true);

DocumentBuilder b=f.newDocumentBuilder();ErHandler eh=new ErHandler();// let the user defined class handle parsing errors b.setErrorHandler(eh);/* parse the XML document.If the document does not meet the DTD rules. anerror will be generated. Three level of errors may occur: warning, error and fatal*/

doc=b.parse(xmlFile);

out.setText(xmlFile+ " is valid document");}catch(ParserConfigurationException e){out.setText("ParserConfigurationException "+e.getMessage());

16

Page 17: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

}catch(SAXException se){out.setText("SAXException "+se.getMessage());}catch(Exception ioe){out.setText("IOException "+ioe.getMessage());}

}

public static void main(String args[]){

DOMValidator dv=new DOMValidator();

}

// inner class to handle the errorsclass ErHandler extends DefaultHandler{public ErHandler(){}public void error(SAXParseException spe){

out.setText("Error "+spe.getMessage());

}public void fatalError(SAXParseException spe){

out.setText("Fatal Error "+spe.getMessage());

}public void warning(SAXParseException spe){

out.setText("Warning "+spe.getMessage());

}

}}

17

Page 18: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

Following is a screenshot of the application:

 Ex4: DTD validation with XML reader

 In this example will create a .NET  application that can validate an XML document against a DTD document. The application will use the xmlTextReader in conjunction with the xmlValidatingReader

 Create a C#.NET application called XMLValidator with the following GUI:

18

Page 19: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

Following is a description of each control:

Control Type Control Name PropertiesForm frmXMLValidator Text="XML DTD Validator "Button btnXML Text="Open XML File "Text Box txtXML  Button btnValidate Text="Validate"

TextBox txtOutputText=""

MultiLine=true

Following is the source code for the application. The bolded face code need to be added only. The rest should be generated by .NET IDE :

frmXMLVAlidator.cs

using System;using System.Drawing;

19

Page 20: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Xml;using System.Xml.Schema;

namespace XMLValidator{/// <summary>/// Summary description for Form1./// </summary>public class frmXMLValidator : System.Windows.Forms.Form{private System.Windows.Forms.Button btnXMLFile;private System.Windows.Forms.TextBox txtXMLFile;private System.Windows.Forms.GroupBox fraOption;private System.Windows.Forms.RadioButton optDTD;private System.Windows.Forms.RadioButton optSchema;private System.Windows.Forms.TextBox txtOutput;private System.Windows.Forms.Button btnValidate;/// <summary>/// Required designer variable./// </summary>private string fileName;private System.ComponentModel.Container components = null;

public frmXMLValidator(){//// Required for Windows Form Designer support//InitializeComponent();

//// TODO: Add any constructor code after InitializeComponent call//}

/// <summary>/// Clean up any resources being used./// </summary>protected override void Dispose( bool disposing ){if( disposing ){

20

Page 21: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

if (components != null) {components.Dispose();}}base.Dispose( disposing );}

#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){this.btnXMLFile = new System.Windows.Forms.Button();this.txtXMLFile = new System.Windows.Forms.TextBox();this.fraOption = new System.Windows.Forms.GroupBox();this.optSchema = new System.Windows.Forms.RadioButton();this.optDTD = new System.Windows.Forms.RadioButton();this.txtOutput = new System.Windows.Forms.TextBox();this.btnValidate = new System.Windows.Forms.Button();this.fraOption.SuspendLayout();this.SuspendLayout();// // btnXMLFile// this.btnXMLFile.Location = new System.Drawing.Point(8, 16);this.btnXMLFile.Name = "btnXMLFile";this.btnXMLFile.Size = new System.Drawing.Size(120, 32);this.btnXMLFile.TabIndex = 0;this.btnXMLFile.Text = "Open XML File";this.btnXMLFile.Click += new System.EventHandler(this.btnXMLFile_Click);// // txtXMLFile// this.txtXMLFile.Location = new System.Drawing.Point(136, 24);this.txtXMLFile.Name = "txtXMLFile";this.txtXMLFile.Size = new System.Drawing.Size(216, 20);this.txtXMLFile.TabIndex = 1;this.txtXMLFile.Text = "";// // fraOption// this.fraOption.Controls.AddRange(new System.Windows.Forms.Control[] {this.optSchema,

21

Page 22: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

this.optDTD});this.fraOption.Location = new System.Drawing.Point(8, 72);this.fraOption.Name = "fraOption";this.fraOption.Size = new System.Drawing.Size(176, 40);this.fraOption.TabIndex = 2;this.fraOption.TabStop = false;this.fraOption.Text = "Option";// // optSchema// this.optSchema.Location = new System.Drawing.Point(88, 16);this.optSchema.Name = "optSchema";this.optSchema.Size = new System.Drawing.Size(80, 16);this.optSchema.TabIndex = 1;this.optSchema.Text = "Schema";// // optDTD// this.optDTD.Checked = true;this.optDTD.Location = new System.Drawing.Point(16, 16);this.optDTD.Name = "optDTD";this.optDTD.Size = new System.Drawing.Size(64, 16);this.optDTD.TabIndex = 0;this.optDTD.TabStop = true;this.optDTD.Text = "DTD";// // txtOutput// this.txtOutput.Location = new System.Drawing.Point(40, 208);this.txtOutput.Multiline = true;this.txtOutput.Name = "txtOutput";this.txtOutput.Size = new System.Drawing.Size(232, 128);this.txtOutput.TabIndex = 4;this.txtOutput.Text = "";// // btnValidate// this.btnValidate.Location = new System.Drawing.Point(208, 80);this.btnValidate.Name = "btnValidate";this.btnValidate.Size = new System.Drawing.Size(120, 32);this.btnValidate.TabIndex = 4;this.btnValidate.Text = "Validate";this.btnValidate.Click += new System.EventHandler(this.btnValidate_Click);// // frmXMLValidator// 

22

Page 23: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);this.ClientSize = new System.Drawing.Size(360, 373);this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnValidate,this.txtOutput,this.fraOption,this.txtXMLFile,this.btnXMLFile});this.Name = "frmXMLValidator";this.Text = "XML Validator";this.Load += new System.EventHandler(this.frmXMLValidator_Load);this.fraOption.ResumeLayout(false);this.ResumeLayout(false);

}#endregion

/// <summary>/// The main entry point for the application./// </summary>[STAThread]static void Main() {Application.Run(new frmXMLValidator());}

private void frmXMLValidator_Load(object sender, System.EventArgs e){

}

private void btnXMLFile_Click(object sender, System.EventArgs e){

try{OpenFileDialog of=new OpenFileDialog();

DialogResult dr=of.ShowDialog();

if (dr==DialogResult.Cancel)MessageBox.Show ("No file has been selected","Error",MessageBoxButtons.OK);else{fileName=of.FileName;

23

Page 24: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

if (fileName=="" || fileName==null)MessageBox.Show ("Invalid file name","Error",MessageBoxButtons.OK);else{txtXMLFile.Text=fileName;}} }catch(Exception ex){txtOutput.Text=ex.ToString();}

}

private void btnValidate_Click(object sender, System.EventArgs e){try{// create an instance of XmlTextReaderXmlTextReader tr=new XmlTextReader(fileName);// create an XmlValidatingReader based on an XmlTextReader instanceXmlValidatingReader vr=new XmlValidatingReader(tr);

vr.ValidationType=ValidationType.DTD;

// create an instance of ValidationHandler classValidationHandler vh=new ValidationHandler(); vr.ValidationEventHandler+=new ValidationEventHandler(vh.validationCallBack);while (vr.Read()){}vr.Close();tr.Close();if (vh.result)txtOutput.Text="validation was successfull ";elsetxtOutput.Text ="validation was unsuccessfull :" +vh.message;}catch(XmlException xe){txtOutput.Text="XMLException "+ xe.ToString();}catch(Exception ex){txtOutput.Text="Exception "+ ex.ToString();

24

Page 25: Introduction to XML - munro.humber.camunro.humber.ca/~zouri/cpan560/cpan560-lec6.doc  · Web viewAny number of XML documents that share the same XML vocabulary is known as a . document

}}}}

Following is the source code for ValidationHandler.cs :

using System;using System.Xml.Schema;namespace XMLValidator{/// <summary>/// Summary description for ValidationHandler./// </summary>class ValidationHandler{// determine if a validation error has occurredpublic bool result=true;// string to hold information about the validation error(s)public string message;public void validationCallBack(object sender,ValidationEventArgs args){message="Validation Error: "+args.Message+"\n";result=false;}}}

 

 

 

25