When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Wednesday, May 10, 2000

Getting Started with XML, Part 2

  • Read Part 1

  • In Part 1 we looked at a basic XML file that stored widget information. In this part we'll look at how to use the XMLDOM to parse through the XML file created in Part 1.

    - continued -

    Microsoft provides the XML DOM (Document Object Model) to assist with parsing an XML document. This DOM can be accessed through the browser if it is an XML-enabled browser (Internet Explorer 5.0) using client-side script; the XML DOM is also available on the server-side. To create an instance of the XML DOM, use the following line of code:

    Dim objXMLDOM
    Set objXMLDOM = Server.CreateObject("Microsoft.XMLDOM")

    Internet Explorer 5.0 must be installed on your Web server for this to work. If it is not you will get an error when trying to create an instance of the Microsoft.XMLDOM component.

    There are a ton of methods, properties, and events for the XML DOM, all of which can be found on Microsoft's XML Documentation Site. For the remainder of this article we will be using the XML document created in Part 1, widget.xml. To load a document into the XML parser, use the load method like so:

    Dim objXMLDOM
    Set objXMLDOM = Server.CreateObject("Microsoft.XMLDOM")
    'load the widget.xml document

    Since the load method expects the physical path of the XML document, we used the Server.MapPath method.

    Then, use a number of the XML methods to iterate through the document. When loaded, the root node becomes the active node. In the widget.xml XML file, the widgets tag was the root node. We can use the childElements collection to iterate through all of the root node's children like so:

      for each objChild in objXMLDOM.documentElement.childNodes
        Response.write objChild.NodeName & " - "    
        response.write objChild.Text & "<BR>"

    The NodeName property displays the name of the node (widget) while the Text property lists the contents of the node. The XML DOM is a very collection-intensive tool - when iterating through an XML document you usually will need to visit each node; each node will have a collection of children nodes, which, in turn, might each have a collection of children nodes themselves, and so on... For more information on these collections it is highly recommended that you check out Microsoft's XML Documentation Site.

  • See a live demo of the code presented above!

    Happy Programming!


  • View the widget.xml XML document

    Recommended Articles/Sites/Books:

  • XML101.com
  • Professional XML Design & Implementation
  • Microsoft's XML Documentation Site

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article