Accessing XML Data using ASP, Part 2By Bipin Joshi
In Part 1 we looked at accessing stand alone XML files. In this part, we'll examine three other alternatives to accessing XML data!
Creating XML files from ground up
Some times you may want to build entire XML document in memory by yourself. This can be the case if your data is stored in RDBMS and you want to convert in into XML only for processing. You can create XML in memory in two ways:
1. Using XMLDOM methods like
2. Directly loading XML data in the form of a string
Let's look at using the XMLDOM first:
The code for loading the XML data from a string appears as follows:
Sending XML data to client
We will consider following three cases:
1. You have generated a full XML document and sending it to the client as is. Here you can include a
stylesheet reference in the XML itself and browser will apply that stylesheet to your document. This requires
that the client browser supports XML.
2. You have generated XML data but want to send it as HTML to the client. Here you can generate a simple HTML by applying a stylesheet at server side.This do not enforce client browser support for XML
3. You want to send XML data as Data Islands to the client. Here you can use
<XML>tag with an ID attribute or
<SCRIPT LANGUAGE=XML>. IE also provides a way to directly bind data islands to HTML elements, liks TABLEs.
Here we set
text/xml so that an XML-aware client browser knows what
to do with it. We can also send raw XML in the form of string:
Finally, using the third method (using the
with an ID attribute or
<SCRIPT LANGUAGE=XML>), we have:
Now, you can bind a table to this data island like this:
The table will display all the rows from the data island automatically.
Storing XML data
Suppose you want to save XML data - generated by you or posted by a client - to a disk file. This is not too difficult, simply call the
save() method of Document object as follows.
Note: With IIS 5.0 you can also save the XML data to ASP Response object directly with
mydoc.save(Response). This is useful if you are processing the XML data and sending it
back to client.
Well, I hope you have found this article useful. To summarize, we examined a number of ways to access/create/display XML data from an ASP page. Happy Programming!