Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

ASP VBScript to Render XML

XML (Extensible Markup Language) is a file format used to describe data. It is similar to HTML in that it uses tags, and the tags are delineated with < and > characters. Unlike HTML you can invent your own tags to define your data. Similar to HTML the tags define a DOM (Document Object Model) for the XML document.

Shown below is an example XML document, simplified from a Microsoft Developer Network example.

<?xml version="1.0"?>
<catalog>
   <book>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <price>44.95</price>
      <description>An in-depth look at creating applications with XML.</description>
   </book>
   <book>
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <price>5.95</price>
      <description>A former architect battles corporate zombies.</description>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <price>5.95</price>
      <description>Young survivors lay the foundation for a new society.</description>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <price>5.95</price>
      <description>Oberon helps to create a new life for the inhabitants of London</description>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <price>5.95</price>
      <description>Two daughters of Maeve battle for control of England.</description>
   </book>
</catalog>

To render the XML DOM for this file, you first create an instance of an XML parser object. If you're using VBScript from an Active Server Page (ASP), you use Server.CreateObject as shown below.

Set xmlDoc = Server.CreateObject( "Microsoft.XMLDOM" )
xmlDoc.async = False

By setting the document's async property to False, the parser will not return control to your code until the document is completely loaded and ready for manipulation. Next use the parser object's Load method, passing the filenale to the Server.MapPath method as shown below.

xmlDoc.Load (Server.MapPath("catalog.xml"))

How to Traverse an XML Document

To traverse an XML document use the documentElement.selectNodes method to create a nodeList object. In the example XML document shown above catalog is the root node.

Set nodeList = xmlDoc.documentElement.selectNodes("catalog")
For Each node In nodeList
   document.write(node.text & "<br />")
Next

You can select a single item in the XML file using the documentElement.selectSingleNode method as shown below.

Set node = xmlDoc.documentElement.selectSingleNode("catalog/book/title['Midnight Rain']")
document.write(node.text)

This selects the the book element that has a title element with a value of 'Midnight Rain'.

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro

Fire HD
[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2010 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269