Accessing XML Data using ASP
By Bipin Joshi
XML Information |
---|
To learn more about XML be sure to check out the 4Guys XML Article Index! It contains a number of links to great XML articles and tuturials around the Web! |
This article covers the variety of ways that XML data can be accessed from an ASP page. Before we delve into these, let's look at some of the various ways XML data can be stored. Some of these storage methods include:
- Simple XML-formatted text files stored in file system
- Data stored in RDBMS that you want to convert to and from XML only when processing the database information
- XML data built dynamically in memory during processing.
We will consider the following cases with code samples of each:
- Accessing stand alone XML files - You can access and manipulate XML files already presented on the
server
- Creating XML files from ground up in memory - You can create entire XML document in memory and
then manipulate it.
- Sending XML data to client - Once done you can send the result of your processing to the client
as XML document or XML Data Islands
- Storing XML data - The data send by client in the form of HTML forms or XML can be processed and stored at server end in the form of XML files. The data can also be saved in Response object directly (which is then send back to the client).
Document Object Model
Document Object Model or DOM plays an important role in accessing XML data. DOM
is governed by W3C and hence is a standard for accessing XML documents. DOM views
XML document as a tree of nodes. Each node of the tree can be accessed randomly.
The main advantage of DOM is that it provides all the functionality in an Object
based way which make it very easy to use. Microsoft has created XML parser based
on DOM known as MSXML. We will be using this component to access the XML
documents. For working with examples presented in tis article you must have
Internet Explorer 5+ installed on your machine(this automatically installs
MSXML.dll
on the machine). It is assumed that you are familier with XML DOM
objects and how to use them via some scripting language.
Accessing stand alone XML files
Your XML data can be stored in separate disk files. In this case we need to perform following steps:
async
property of document objectLet's look at a code example!
Step 1: Create object of Microsoft.XMLDOM
Dim mydoc
|
Step 2: Set async property
If you set this property to false then DOM will prevent access to the document while it is in process of
change or update. If you set this property to true then you need to use onreadystatechange
event and check the readyState
property for complete(4)
.
mydoc.async=false
|
Step 3: Load XML file
mydoc.load("file_path")
|
Note: With IIS 5.0 you can also load XML data directly from ASP Request object if it is being send from client as follows :
' Load the specified XML file
|
Step 4: Check for any errors
Here, you check the errorcode property and decide the success or failure of the operation.
|
Now,you can use various XMLDOM methods and properties to manipulate the document.
In Part 2 we'll look at the three other ways to access XML data through an ASP page...