A Brief Introduction to XML DOM in MSXML
copyright Bun Yue 3/30/2000

Introduction

To invoke MSXML and parse a file:

<%
   Set objXML = Server.CreateObject("Microsoft.XMLDOM")
   objXML.async = False                 ' optional
   objXML.validateOnParse = False       ' optional
   objXML.Load(Server.MapPath("scriptingNews.xml"))
%>

Notes:

  1. Server.CreateObject("Microsoft.XMLDOM") returns an XMLDOMDocument object, representing the entire XML document.
  2. The async property may be set to false so that future loading will not be asynchronous.  The next statement will wait until the loading is completed.
  3. The validateOnParse property may be set to false to disable DTD validation.  This will speed up the parsing.
  4. MapPath performs mapping of file name of virtual directory to physical directory file name.
There are four major classes:

XMLDOMDocument Object

XMLDOMNode Object Example:

For the parsed XML document with top elements <news>:

<news>
      <article id="_6337363">
         <headline_text>CDnow: Harbinger of E-Commerce Doom?</headline_text>
         <source>Industry Standard</source>
         <media_type>text</media_type>
         <cluster>E-commerce news</cluster>
         <tagline> </tagline>
         <document_url>http://www.thestandard.com/news/grok/</document_url>
         <harvest_time>Mar 30 2000 10:23PM</harvest_time>
         <access_registration> </access_registration>
         <access_status> </access_status>
      </article>
      <article id="_6336605">
        <headline_text>Tommy Hilfiger And Bluefly Settle Suit</headline_text>
         <source>Gomez</source>
         <media_type>text</media_type>
         <cluster>E-commerce news</cluster>
         <tagline> </tagline>
         <document_url>http://www.gomez.com/features/gomezwire.cfm?topcat_id=0&amp;section=ALL</document_url>
         <harvest_time>Mar 30 2000  9:58PM</harvest_time>
         <access_registration> </access_registration>
         <access_status> </access_status>
      </article>
</news>

To list all headline texts:

Method #1:

Set objLst = objXML.getElementsByTagName("article")
For i = 0 to (objLst.length - 1)
    strHeadline = objLst.item(i).getElementsByTagName("headline_text").item(0).childNodes(0).nodeValue
    Response.write "News item #" & (i+1) & ": " & strHeadline & "<BR>" & vbCRFL
Next

Method #2 (not so good):

Set objLst = objXML.getElementsByTagName("article")
For i = 0 to (objLst.length - 1)
    strHeadline = objLst.item(i).childNodes(0).text
    Response.write "News item #" & (i+1) & ": " & strHeadline & "<BR>" & vbCRFL
Next

Method #3:

Set objLst = objXML.getElementsByTagName("headline_text")
i = 0
For each objNode in objLst
    strHeadline = objnode.text
    i = i + 1
    Response.write "News #" & i & ": " & strHeadline & "<BR>" & vbCRFL
Next

XMLDOMNodeList Object

XMLDOMNamedNodeMap Example:

To print all id attributes of attribute elements.

Set objLst = objXML.getElementsByTagName("article")
For i = 0 to (objLst.length - 1)
    strId = objLst.item(i).attributes.getNamedItem("id").nodeValue
    Response.write "News #" & i & " id: " & strId & "<BR>" & vbCRLF
next