Article

XML and Web Services for Microsoft Developers - Part 1

Page: 1 2 3 4 5 6 Next

Using MSXML on the Server Side

MSXML is a COM-based API and hence can be used from scripting languages such as VBScript, ECMAScript, and Perl. In this example, we'll write VBScript code inside an ASP page, use MSXML DOM to load an XML document, and create HTML response to be sent to the client browser.

Let's say you have the following XML document available as sites.xml under the same IIS virtual directory as the ASP page:

<?xml version='1.0' encoding='utf-8'?>  
<Sites>  
<Site>  
<URL>www.SitePoint.com</URL>  
<Title>SitePoint Hub</Title>  
</Site>  
<Site>  
<URL>www.WebmasterBase.com</URL>  
<Title>Build your Site</Title>  
</Site>  
<Site>  
<URL>www.eCommerceBase.com</URL>  
<Title>Make Money</Title>  
</Site>  
<Site>  
<URL>www.PromotionBase.com</URL>  
<Title>Increase Traffic</Title>  
</Site>  
<Site>  
<URL>www.ServicesBase.com</URL>  
<Title>Web Tools and Services</Title>  
</Site>  
<Site>  
<URL>www.SitePointForums.com</URL>  
<Title>Help Community</Title>  
</Site>  
</Sites>

The following ASP code uses MSXML 4.0 DOM to load the sites.xml, process it and generate HTML output. If you do not already have MSXML 4.0 installed, download and install it from the MSDN Website.

ShowSites.asp  
<%@ Language=VBScript %>  
<%  
Option Explicit  
Response.Expires = -1  
 
'Create MSXML 4.0 DOMDocument instance  
Dim objXMLDoc  
Set objXMLDoc = Server.CreateObject("MSXML2.DOMDocument.4.0")  
 
'Load sites.xml file present in the same directory as this ASP page  
objXMLDoc.async = False  
objXMLDoc.validateOnParse = False  
objXMLDoc.resolveExternals = False  
 
Dim bLoadResult  
bLoadResult = objXMLDoc.load(Server.MapPath("sites.xml"))  
 
'If Load successful  
If bLoadResult Then  
'Generate HTML Output  
 
'Select Site Nodes  
Dim siteNodes  
Set siteNodes = objXMLDoc.selectNodes("/Sites/Site")  
 
Response.Write "<b>SitePoint: <i>Empowering Web  
Developers since 1997.</i></b><ul>"  
 
'For each Site node  
Dim aSiteNode  
For Each aSiteNode in siteNodes  
With Response  
.Write "<li><a href='http://"  
.Write aSiteNode.selectSingleNode("URL").nodeTypedValue  
.Write "'>" & aSiteNode.selectSingleNode("Title").nodeTypedValue  
.Write "</a></li>"  
End With  
Next  
Response.Write "</ul>"  
 
Else  
'Load Unsuccessful, print error  
Response.Write "<font color='red'>" &  
objXMLDoc.parseError.reason & "</font>"  
End If  
 
%>

The above ASP page begins by creating an instance of class DOMDocument using the MSXML 4.0 version dependent ProgID MSXML2.DOMDocument.4.0. Next, we set certain properties to have the XML document loaded synchronously, and tell MSXML not to validate XML document and skip resolving any external references in the XML document.

As the document is being loaded from an external file, we use the load method, instead of loadXML which is used to load the XML document from a string. If the document load succeeds, we use the DOM selectNodes methods and pass it the XPath expression /Sites/Site that selects all the <Site> nodes from the source XML document. Finally, for each <Site> node, we assume that it contains <URL> and <Title> child nodes, and we select these node values to generate the HTML output.

If the document load fails, the error message string is generated using parseError interface.

Download the source code for this article, save the included XML (sites.xml) and ASP page (ShowSites.asp) under an IIS virtual directory, browse to ShowSites.asp and you should see the output similar to following screen:

950_screen1

Figure 1. HTML output generated by an ASP page using MSXML DOM

In this example, you learned about using MSXML DOM in an ASP page on the server side. Let's now see an example of using MSXML in a Visual Basic application to be run on the client side.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links