
This tutorial is aimed at giving you a crash course in reading XML files with ASP and the XML Document Object Model. It is assumed that you have a basic understanding of what an XML document is, what one looks like and what they contain. Please visit http://www.w3schools.com for a tutorial on learning XML.
This document is broken down into a number of steps for you to follow in order to help you grasp the concepts being detailed here.
The first thing you obviously need, is an XML file. You will also need to know how the XML document is structured, and in what order the desired items elements appear in as you will be referencing them via an index number. For this tutorial, we will use the XML file below.
The XML file below is a simple, custom made XML file that details a Guitar Tablature Catalog that keeps track of what printed tablatures someone owns.
<?xml version="1.0"?>
<TabCatalog>
<tab>
![]()
<artist>Joe
Satriani</artist>
![]()
<tabtitle>Always
with me, Always with You</tabtitle>
![]()
<pagecount>6</pagecount>
</tab>
<tab>
![]()
<artist>Steve
Vai</artist>
![]()
<tabtitle>Babushka</tabtitle>
![]()
<pagecount>12</pagecount>
</tab>
<tab>
![]()
<artist>Eric
Clapton</artist>
![]()
<tabtitle>Tears
in Heaven</tabtitle>
![]()
<pagecount>10</pagecount>
</tab>
</TabCatalog>
The above XML file, as mentioned, outlines a simple Guitar Tablature Catalog that contains details of Three Guitar Tabs. In this file, a guitar tab is defined as a <tab>...</tab> element within the catalog. The catalog itself is defined by the <TabCatalog>...</TabCatalog> element.
Type up this XML file and save it as guitartab.xml.
Now that we have our XML file, we need to not the information we required. We can see that for Each <tab> element, there are three child elements and they are always in the same order of:
This is important to note the order of these child elements when we are writing our XML reader script. In this case, <artist> = 0, <tabtitle> = 1 and <pagecount> = 2.
Now we can get on with writing our ASP script.
In this part of the tutorial, we shall write our ASP code to read from the new XML file. The first thing we need to do is to invoke an instance of the Microsoft XML DOM object on the server, and configure it to open the new XML file we created in the previous step. In this step, we shall also defined the other variables and objects we need for the program.
Create a new text file and enter the code below. Save the file as 'tabreader.asp'.
<%
Dim objXML 'the XML object
Dim objItemList 'to hold a list of xml elements
Dim objItem 'used to indicate the current item in the xml list
'create an instance
of the Microsoft XML DOM
Set objXML = Server.CreateObject("Microsoft.XMLDOM")
'configure the Microsoft XML DOM instance
objXML.async = False
objXML.setProperty "ServerHTTPRequest", True
'load out XML feed into the XML DOM instance
objXML.Load(server.MapPath("guitartab.xml"))
As you can see, the code above is commented so that you should be able to understand what is going on in this section of the code. The only two lines of importance here are the creation of the objXML intance, and invoking the Load function of it to load our XML page from the server.
Note that we have to use the Server.MapPath("guitartab.xml") function in order for the objXML object to find the required file.
Once you have entered that code in to a new file, we can proceed with the next step, which is checking for XML Parsing errors. This is an essential step to take in writing XML reader scripts as if there is an error with the XML document being read, it can lead to all sorts of problems during interpretation by the objXML object.
Add the code below to the code above.
'now we check for parser errors and report
any.
If objXML.parseError.errorCode <> 0 Then
![]()
Response.Write "<pre>" & vbCrLf
![]()
Response.Write "<strong>Error:</strong> " & objXML.parseError.reason
![]()
Response.Write "<strong>Line:</strong> " & objXML.parseError.line & vbCrLf
![]()
Response.Write "<strong>Text:</strong> " _
![]()
& Server.HTMLEncode(objXML.parseError.srcText) & vbCrLf
![]()
Response.Write "</pre>" & vbCrLf
End If
All this block of code does is check the parseError.errorCode property of our objXML instance to see if it encountered any problems whilst reading the XML document. We do this using a conditional if statement. If the return value is not equal to 0, then an error was found and we need to display what the error was by using the error details obtained by referencing the objXML.parseError.reason and objXML.parseError.line properties of our objXML instance.
All we do with these error details is output to the user.
Now we reach the final part of our ASP XML reader. Now that we have successfully loaded and parsed the XML document with the objXML instance, we can retrieve a list of specific elements to display to the user.
The only elements of our custom XML document we are interested in here are the <tab> elements, so we can call the getElementsByTagName() method of our objXML instance to retrieve them. like this:
'now we loop through the XML documents
'TAB' elements
Set objItemList = objXML.getElementsByTagName("tab")
Add this code to your ASP document.
Now all that is left to do is to loop through the collection of elements returned to the objItemList object and display their properties. Add this final code to your ASP document.
For Each objItem In objItemList
![]()
response.write("<b>Artist:</b> " & objItem.childNodes(0).text & "<br>")
![]()
response.write("<b>Song Title:</b> " & objItem.childNodes(1).text & "<br>")
![]()
response.write("<b>Page Count:</b> " & objItem.childNodes(2).text & "<br>")
![]()
response.write("<hr>")
next
'close the XML DOM instance
Set objXML = Nothing
'clear the object list
Set objItemList = Nothing
%>
As you can see from this last block of code, we can use the For Each X in Y style loop to iterate through the returned list objects and display each elements properties. To retrieve an elements child elements, we can call the the childNodes() property of the current item.
Now, remember when I said you needed to memo rise the index values of each child element in the <tab> elements? well this is where that comes into play. We need to pass an index value to the .childNodes property in order to get access to the child element.
If you remember, element 0 was the artists name, element 1 was the song title and element 3 was the page count for the tab. We need to use this knowledge when displaying headings in the output as you can see from the code above.
You should now have a complete ASP file that matches the following:
<%
Dim
objXML 'the XML object
Dim
objItemList 'to hold a list of xml elements
Dim
objItem 'used to indicate the current item in the xml list
'create
an instance
of the Microsoft XML DOM
Set
objXML = Server.CreateObject("Microsoft.XMLDOM")
'configure
the Microsoft XML DOM instance
objXML.async
= False
objXML.setProperty "ServerHTTPRequest",
True
'load
out XML feed into the XML DOM instance
objXML.Load(server.MapPath("guitartab.xml"))
'now
we check for parser errors and report any.
If
objXML.parseError.errorCode <> 0 Then
![]()
Response.Write "<pre>" & vbCrLf
![]()
Response.Write "<strong>Error:</strong> " & objXML.parseError.reason
![]()
Response.Write "<strong>Line:</strong> " & objXML.parseError.line & vbCrLf
![]()
Response.Write "<strong>Text:</strong> " _
![]()
& Server.HTMLEncode(objXML.parseError.srcText) & vbCrLf
![]()
Response.Write "</pre>" & vbCrLf
End
If
'now
we loop through the XML documents 'TAB' elements
Set
objItemList = objXML.getElementsByTagName("tab")
For
Each objItem In objItemList
![]()
response.write("<b>Artist:</b> " & objItem.childNodes(0).text & "<br>")
![]()
response.write("<b>Song
Title:</b> " & objItem.childNodes(1).text & "<br>")
![]()
response.write("<b>Page
Count:</b> " & objItem.childNodes(2).text & "<br>")
![]()
response.write("<hr>")
next
'close
the XML DOM instance
Set
objXML = Nothing
'clear
the object list
Set
objItemList = Nothing
%>
You can now save this code and upload it to the same location as the XML document. When you run the script, you should see the following output on the screen:

Congratulations! You can now read XML files using ASP!
If you see any parser errors, you need to correct your XML file. If you see any Server errors, you will need to fix the ASP file. Compare all of your code to that on this page and upload it again.
Tutorial by Justin Kercher
2005.
This document may not be copied for use on another site.
Copyright© 2005.