Accessing XML Data using ASPBy Bipin Joshi
|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
- 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:
asyncproperty of document object
Let's look at a code example!
Step 1: Create object of Microsoft.XMLDOM
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
event and check the
readyState property for
Step 3: Load XML file
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 :
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...