|
Reading XML file with ASP by Faisal Khan.
Uses of XML
XML stands for Extensible Markup Language. XML can be used in many ways and one of
which is 'data storage'. This is the one we will be exploring in this article. XML
along with XSL ( Extensible Stylesheet Language ) can by used to present data on the
web pages. XML provides the data and XSL allows us to present it the way we want. Remember
though that not all browsers support XML on the client side, only Micrsoft Internet
Explorer 5.0 and above support XML. XML can also be used to perform RPC ( Remote
Procedure Call ). Actually this capability of XML to allow communication between distant
applications is so strong that Microsoft has developed SOAP ( Simple Object Access
Protocol ) specification which uses XML to allow communication between remote
applications. XML can be used for a lot more purposes which I haven't mentioned here.
Also keep in mind that Microsoft's future .NET platform will make use of XML even more
than any tool does today. ASP+, ADO+ and others will use XML to define and present data.
So if you are comfortable with XML today, it will help you in the near future when you
will be getting yourself ready to develop applications in the revolutionary platform
called Microsoft .NET.
What are XML files ?
If you know HTML then you already know 70% XML. XML is tag based just like HTML. The
major difference between HTML and XML is that in XML you can define your own tags while
in HTML you make use of pre-defined tags. XML files most often than not begin with following
tag on top of the page :
<?xml version="1.0"?>
This line tells the XML parser the version of XML we are using. You for now don't
need to change it, just remember to add it on top of every XML page you create.
Every starting XML tag should have a corresponding end tag.
<name>Faisal Khan</name>
If you don't want to write end tag then simply add forward slash in the tag like
this :
<br/>
Thus <br> tag of HTML will be written as <br/> in XML.
Elements
The tags in XML are known as 'elements'. 'elements' may or may not contain any
content, thus following are all correct :
<name>Faisal Khan</name>
<name></name>
<name/>
Attributes
Attributes are the same name / value pairs that you use in HTML. Elements may or
may not contain attributes. Following is an example of an element containing an
attribute :
<name language="US-EN">Faisal Khan</name>
This was a very simple and basic introduction to XML, on the next page we will see
what is the difference between well formed and valid XML documents.
Creating 'Page.xml' file
We will now create a simple XML file; 'page.xml'. Open notepad and
create a new file 'page.xml' and then copy paste the following code into it :
<?xml version="1.0"?>
<main>
<title>Our Page.xml file</title>
<heading>This is a test heading</heading>
<paragraph>This is our paragraph and you can write whatever
you want to in this space.</paragraph>
<testHTML><![CDATA[We will enclose some HTML code in CDATA section now :<br>
<table width="60%" border="1" bordercolor="silver" cellspacing="2"
cellpadding="3">
<tr>
<td>
You can write any HTML code or for that matter any type of text inside
the CDATA section without a fear of getting any error.<br><br>
Note if we write this without the CDATA tags, the xml parser will
raise an error and won't show the document.
</td>
</tr>
</table>]]></testHTML>
</main>
There are certain points to be noted in the code above. Notice that after writing the
xml version line we have enclosed our three elements (title, heading, paragraph, testHTML)
inside a single tag (main). This is so because in XML after the processing instructions
(xml version tag in the beginning) all the elements and sub-elements that we define have
to be enclosed within a single element whatever you name it. Thus in our case we defined
that single element and named it 'main' which encloses our other 4 elements.
Notice that in the 'testHTML' tag we have put lot of our normal HTML code. XML parser
will raise an error when it encounters HTML code which doesn't abide to XML rules. To
get through this we make use of <![CDATA[....]]> markup
to tell the XML parser that the following text is character data and should not be
parsed and evaluated like other XML data is done. CDATA actually stands for 'character data' and
allows any kind of characters / text to be written inside it. So remember that whenever
you want to enclose HTML inside your XML elements, enclose it between the CDATA markup tags.
Rest of the code is easier to understand. In the first line we defined the processing
instruction by telling the XML version to the XML parser. Then we created a 'main' tag
element to enclose rest of our XML document. In the 'main' element we created 'title',
'heading', 'paragraph' and 'testHTML' tags to contain some text which will be read by our
ASP page.
Creating 'showxml.asp' page
We will now create a very simple ASP page and code it to show the XML data that
we created in the 'page.xml' file. Open notepad and create a new ASP page. Then copy
paste the following code into it and save it :
<%
Option Explicit
Response.Buffer = True
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.load (Server.MapPath("page.xml"))
Dim title, heading, paragraph, testHTML
title = xml.documentElement.childNodes(0).text
heading = xml.documentElement.childNodes(1).text
paragraph = xml.documentElement.childNodes(2).text
testHTML = xml.documentElement.childNodes(3).text
Set xml = Nothing
%>
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h3 align="center"><%= heading %></h3>
<p align="center"><% = paragraph %></p>
<div align="center"><%= testHTML %></div>
</body>
</html>
Put 'showxml.asp' page in the same directory where you have kept the 'page.xml' file.
Explanation :
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
We create the XML Document Object in the 'xml' variable.
xml.async = False
By setting '.async' option to False, we are saying to the XML parser to show the data
as soon as it begins to read it. Note that by setting '.async' to False, retrieving of
XML data is speeded up.
xml.load (Server.MapPath("page.xml")
Next we load the 'page.xml' file. The 'load()' method asks for complete physical
path to the XML file. We give it the complete physical path by using 'Server.MapPath'
method to convert the relative path to complete physical path.
Dim title, heading, paragraph, testHTML
title = xml.documentElement.childNodes(0).text
heading = xml.documentElement.childNodes(1).text
paragraph = xml.documentElement.childNodes(2).text
testHTML = xml.documentElement.childNodes(3).text
|