|
| |
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
Topic : Grabbing XML data from ASP
|
|
Author : Jason
|
Total Visits: 100
|
|
Published Date:
Tuesday, April 03, 2001
|
|
|
Grabbing XML data from ASP
By now, most of our readers are familiar, at least in principle, with XML - the w3c's platform neutral data markup language. However not everyone knows how to use XML with ASP. In this article, I'll run you through using ASP and JScript to open and read XML files. in the next article, I'll show you how to create and modify XML with ASP.
The example I'm going to show you is in use on my office intranet, for the purposes of identifying who is logged in. since all the machines in the office have a static, unique IP, I've used Request.ServerVariables("remote_host") to identify the machine, and XML to map machine to user. It's a little courtesy app, and also allows me to restrict administrative functions to only five machines in the entire office.
Here's the function
function userName() {
var userIP = new String(Request.ServerVariables("remote_host"));
var objXML = new ActiveXObject("MSXML2.DOMDocument");
objXML.async = false; var userName = '';
objXML.load('d:\\wwwroot\\homepage\\interactive-ips.xml');
var ips = objXML.getElementsByTagName("ip");
var names = objXML.getElementsByTagName("name");
for(var x=0;x<ips.length;x++) {
if(ips.item(x).text.toString()==userIP) {
var userName = names.item(x).text;
}
}
return userName!=''?userName:"unknown"; objXML = null;
}
and here's the XML document (data changed for privacy, most users removed and replaced with .....)
<?xml version="1.0"?>
<root>
.......
<user>
<name>Jason</name>
<ip>204.10.45.72</ip>
<email>jasonb@attik.com</email>
</user>
<user>
<name>Jason at home</name>
<ip>142.141.249.169</ip>
<email>jason@atrax.co.uk</email>
</user>
.....
</root>
Let's zip through the function quickly. The first relevant item is..
var objXML = new ActiveXObject("MSXML2.DOMDocument");
You'll note I haven't used Server.CreateObject - this is because the MSXML2 Component isn't reliant on an ASP context - it can work alone, so i allow it to.
objXML.async = false;
MSXML2 has an asynchronous mode, whereby it doesn't wait to perform some operations before returning some values. With ASP being a client/server model - we don't need this (in fact, can't use it) so we turn it off
objXML.load('d:\\wwwroot\\homepage\\interactive-ips.xml');
Load() takes a filesystem (not url) path to the XML doc you wish to load. the method returns true of false for success or failure, which I haven't implemented here for simplicity's sake
var ips = objXML.getElementsByTagName("ip");
var names = objXML.getElementsByTagName("name");
This initialises two arrays (actually objects), ips and names, allowing me to map one to another with a simple for() loop. note we use ips.item(x).text.toString() to get the string value from the object as we loop through.
I advise you to go and grab the Microsoft XML SDK and do some backround reading in preparation for our next lessons... where we grab XML from another server, and update/edit/create XML on the fly using ASP
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|