To read the article online, visit http://www.4GuysFromRolla.com/webtech/051000-1.2.shtml

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.

    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
    objXMLDOM.load(Server.MapPath("widget.xml"))
    

    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>"
      next
    

    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!


    Attachments:

  • View the widget.xml XML document

    Recommended Articles/Sites/Books:

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


  • Article Information
    Article Title: Getting Started with XML, Part 2
    Article Author: Scott Mitchell
    Published Date: Wednesday, May 10, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/051000-1.2.shtml


    Copyright 2014 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers