
| Tracking Web Page Visitors with XML By Lai Yuen Kit | Rating: 3.8 out of 5 Rate this article |
email this article to a colleague suggest an article| Overview The following code creates an XML file that lists information about the visitors to this page. The code can be found in visitorTrack.zip: 
After we've run the visitorTrack.asp, a new XML file, named "visitor.xml" will be created. Open the file with a browser. We will see the visitor's information (in this case, our information) as below:  How It Works First, we start by creating an instance of an XML object. If you are using MSXML version 3, then remark, or comment out, the first statement and use the second line. 
Now we check for the existence of the file we need, visitor.xml. If the file is found, the objXML.load method will return a Boolean of True, otherwise it will return False. If the file does not exist, we need to create a new file by creating a Processing Instruction (<?xml version="1.0">) and appending a document element (<visitors>). This kind of checking also makes sure that we do not overwrite our records accidentally. The variable intID is used to emulate Primary Key / Identical Column which was found in RDBMS. 
Once we have checked for our XML file, we create another instance, named objXMLv. We create all the XML tags we want (<ID>, <HTTP_USER_AGENT>, etc.) and append it to objXMLv. After that, we can start supplying the values by using Request.ServerVariables properties. 
Now we can append the whole record to the Document Element (in this case, <visitors>). Setting the cloneNode to True will append the "<visitor>" element and its child elements as well. Finally we can save our XML file using the save() method. 
You can test your code at this point. After you have run your code, a new record will be added into visitor.xml. Try to open visitor.xml and see what you get!
Presenting XML Records We'll create an ASP page that presents XML data in a more readable format. Your page should appear as shown: 
The following shows the code needed to generate the required page. 
How It Works We start by creating an instance of XMLDOM and load our visitor.xml into the instance. 
Once again, we check for the existence of the visitor.xml file. If the file cannot be found, we will display "No Record Found." 
Note that I use a "With" statement in the example. By using "With," we reduce the need for object to lookup and speed up the process a certain degree. This also helps us save a few keystrokes and makes the code less error-prone. (For more explanation, see http://msdn.microsoft.com/workshop/server/asp/ASPtips.asp) Variable intMaxRow and variable intMaxCol are used to store the maximum number of rows and the maximum number of columns, respectively. These variables will be used to iterate our records later on. Since any number in an XML object is zero-based, we need to subtract one from that value before we assign the number for "intMaxRow" and "intMaxCol." 
Now we can draw our table. To make our table more useful we will put the field names / tag names in the first row to make headers. Tag names are retrieved using the nodeName property. 
This part will be a little bit tricky. We'll use two For-Next blocks to iterate through the records. Basically the structure of our XML file is analogous to a two-dimensional array. We treat it using the same method used when dealing with two-dimensional arrays. If you have experience using the getrows() method in an ADO object, you can easily understand the concept here. 
Finally, we deference our XML object. 
Conclusion Now we are able to write some simple applications using XML. We can start building a more complex XML-driven Web application. Do bear in mind that, although implementation of XML is an effective yet economic option, storing sensitive information (such as credit card account numbers) in XML format is not recommended, since one can easily access the file without permission.
About the Author Lai Yuen Kit, an ASP freelancer, is well versed in Flash, MS Access and SQL Server database technologies, Photoshop, XML, and, of course, HTML. He is proficient in the English, Chinese, and Malay languages. He can be reached at laiyuenkit@yahoo.com.
|
| | | Supporting Products/Tools |  | Stonebroom.ASP2XML | | Stonebroom.ASP2XML(c) is an interface component designed to make building
applications that transport data in XML format much easier. It can be used
to automatically pass updates back to the original data source. | [Top] |
|
| | Other Articles |  | Sep 22, 2005 - Implementing Remote Calling Without Using AJAX | | Right now the latest buzzword around town is AJAX. AJAX is an acronym for Asynchronous JavaScript and XML and is a method used to implement remote calling. The problem is that AJAX is only implemented in ASP.NET 2.0. This article will show you one way to implement remote calling without using AJAX or the XMLHttpRequest object. The technique outlined can even be used from classic ASP and is sufficient for most remote calling needs. [Read This Article] [Top] |
| | | | Aug 3, 2005 - SQL Server 2005 XQuery and XML-DML - Part 1 | | Microsoft SQL Server 2005 now offers great support for and close integration with XML as a data persistence format. In the first article of his series examining this new support, Alex Homer offers an overview of how SQL Server 2005 stores XML documents and schemas, examines how it supports querying and manipulating XML documents, and provides a simple test application that allows you to experiment with XQuery. [Read This Article] [Top] |
| | | | | Jun 15, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 2 | | Alex Homer continues his series on reading and writing XML in .NET 2.0. In part one, we focused on the reading side of things, examining the XmlReader and XmlReaderSettings classes. In this article, we move on to look at the XmlWriter and XmlWriterSettings classes, and how they can be used to write XML documents and fragments more easily and more efficiently than in version 1.x of .NET. [Read This Article] [Top] |
| | |
| Mailing List Want to receive email when the next article is published? Just Click Here to sign up. |
|