perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
  You are here: home Info Bank Articles » MSXML 3.0 Illustrated Saturday, 23 February 2008
 

Back to Articles Page      

        

MSXML 3.0 Illustrated


Table of contents
Introduction
Few words about MSXML 3.0 Web release
DOM Example
SAX 2.0 Example
XML Data Source Object
XPath Example [Part II]
XSLT Example [Part II]
XDR Schema Example [Part II]
Summary [Part II]


  • Introduction
    XML is becoming more and more popular, so are it's surrounding technologies like XSLT, SOAP, etc. Microsoft is doing good job keeping up with the standards and it was proved when MSXML 3.0 was released in November last year. MSXML 3.0 Web release is a perfect DOM based XML parser, with SAX 2.0 support, full implementation of XSLT/XPath specifications, and more. In this article, we'll take a practical approach, write an example for each and learn how MSXML 3.0 supports these standards.


  • Few words about MSXML 3.0 Web release
    In September 2000, Microsoft announced MSXML 3.0 beta release. After fixing few bugs, improving performance and with minor changes to SAX 2.0 API implementation, in November 2000, Microsoft announced the long-awaited production release of MSXML 3.0. To run all the samples from this article, you'll have to install (and configure) MSXML 3.0. Click here External link to download MSXML 3.0.

    The MSXML 3.0 release download includes three .dll files: msxml3.dll, msxml3a.dll, and msxml3r.dll. The msxml3a.dll and msxml3r.dll files are resource DLLs that enable the MSXML parser to run on multiple platforms. The MSXML 3.0 is installed in side-by-side mode, which means that installing this version of the parser will not cause any previously installed version of MSXML to be replaced. Both the new parser and the old one will reside "side-by-side" on your computer. If you do not want to run the parser in side-by-side mode, you can use a utility called xmlinst.exe External link to run MSXML 3.0 in Replace mode.

    Once you have installed MSXML 3.0 on your machine, click here to test if it is installed correcly.


  • DOM Example
    DOM, or Document Object Model is a specification of programming interfaces, developed by the World Wide Web Consortium (W3C). The DOM defines interfaces that allow programmers to navigate XML documents and also to manipulate their content and structure.

    The DOM allows applications to work with XML document structures and information as program structures rather than text streams. The DOM-based parser loads an XML document as a tree-like hierarchy. Tree nodes represent document content and structures. The DOM programming interfaces enable applications to traverse the tree and manipulate its nodes. MSXML implements DOM and allows to load or create a document; access and manipulate the information and structures contained within the document; and save the document back to an XML file, if necessary.

    To illustrate MSXML's DOM implemntation, we'll first create an XML document, save that as c:\1.xml. In example 2, we'll load 1.xml file as XML DOM Document, update it and save it back to the disk. Let's first look at the XML document that we'll be creating in example 1 and updating in example 2. Here we go...
    <?xml version="1.0" ?>
       <BankAccount>
            <Number>1234</Number>
            <Name>Darshan Singh</Name>
            <Type>Checking</Type>
            <OpenDate>11/04/1974</OpenDate>
            <Balance>25382.20</Balance>
       </BankAccount>

    Let's now use MSXML DOM to first create above XML document. We'll be writing the code in Visual Basic 6.0. Simply create a Visual Basic 6.0 Standard EXE project and include reference (Project | References) to Microsoft XML, v3.0 (MSXML3.DLL). Double click on the form and write following code under Form_Load() method:

    Dim xmldoc As DOMDocument30
    Dim objPI As IXMLDOMProcessingInstruction
    Dim rootElement As IXMLDOMElement
    Dim aElement As IXMLDOMElement

    'Creating DOM Document object
    Set xmldoc = New DOMDocument30

    'XML version processing instruction
    Set objPI = xmldoc.createProcessingInstruction("xml", "version=""1.0""")
    xmldoc.appendChild objPI

    'Creating root element
    Set rootElement = xmldoc.createElement("BankAccount")
    Set xmldoc.documentElement = rootElement

    'Creating and appending Number element
    Set aElement = xmldoc.createElement("Number")
    aElement.nodeTypedValue = "1234"
    rootElement.appendChild aElement

    Set aElement = xmldoc.createElement("Name")
    aElement.nodeTypedValue = "Darshan Singh"
    rootElement.appendChild aElement

    Set aElement = xmldoc.createElement("Type")
    aElement.nodeTypedValue = "Checking"
    rootElement.appendChild aElement

    Set aElement = xmldoc.createElement("OpenDate")
    aElement.nodeTypedValue = "11/04/1974"
    rootElement.appendChild aElement

    Set aElement = xmldoc.createElement("Balance")
    aElement.nodeTypedValue = "25382.20"
    rootElement.appendChild aElement

    'Saving the document to file c:\1.xml
    xmldoc.save "c:\1.xml"

    MsgBox "XML document created and saved to file c:\1.x.xml"

    Set aElement = Nothing
    Set objPI = Nothing
    Set rootElement = Nothing
    Set xmldoc = Nothing

    End

    The above few lines of code illustrate how easy it is to create XML document using MSXML. You can step through the code and when done look at c:\1.xml file.

    The first step is to instantiate a DOMDocument30 object, next we add XML declaration processing instruction using createProcessingInstruction and appendChild methods.

    We then create an IXMLDOMElement object and set it as a root element for our XML document, by updating the documentElement property.

    You'll then notice five blocks of three lines of code - each block creates an element, set it's value and appends the newly created element as child to root element. This way we create Number, Name, Type, OpenDate and Balance nodes. To do this, we first call createElement method, then update nodeTypedValue property and finally call appendChild method.

    Finally, it's now time to save our XML document into file c:\1.xml. To do this we simply call DOMDocument's save method and pass file name with path as parameter.

    Let's now see one more example of how DOM can be used to update XML documents. In the following example, we'll load XML document from c:\1.xml, an XML document file created in previous example, and update the balance by adding 500$ to it. Once again, create a new Visual Basic 6.0 Standard EXE project, add reference to MSXML 3.0 and copy-paste following lines of code:


    Dim xmldoc As DOMDocument30
    Dim aNode As IXMLDOMNode

    'Creating DOM Document object
    Set xmldoc = New DOMDocument30

    'Load XML document
    xmldoc.Load "c:\1.xml"

    'Select Balance Node
    Set aNode = xmldoc.documentElement.selectSingleNode("Balance")

    'Update Balance
    fBal = aNode.nodeTypedValue + 500
    aNode.nodeTypedValue = fBal

    'Save the document back to the disk
    xmldoc.save "c:\1.xml"

    Set aNode = Nothing
    Set xmldoc = Nothing

    Once again, we start by creating the DOMDocument30 object and then we call it's Load method with C:\1.xml as a parameter. MSXML then reads the c:\1.xml file and generates a tree corresponding to our XML document in the memory, so that we can call DOM methods and manage the XML document.

    If you look at our sample XML document, you'll notice that Balance node is directly children of root node. In order to access the Balance node, we use SelectSingleNode method on root node. The documentElement property returns the root node and we call SelectSingleNode method by passing "Balance", the name of the node we want as parameter.
    Next step is to update the node's value (adding 500 to the balance). We do that by using the nodeTypedValue property.

    Final step again is to save the document back to the disk and once again we call save method on DOMDocument30.

    Related links:
    XML and Binary Data [http://www.perfectxml.com/articles/xml/binary.asp]
    MSXML with Internet Explorer 4.0 [http://www.perfectxml.com/articles/xml/msxmlIE4.asp]


  • SAX 2.0 Example
    As we learned, when we call Load method on DOMDocument30, it loads the entire XML document into memory as tree. This can be probelmatic if you have very large XML documents or memory limitations. Also, once you've called Load method there is no way to abort parsing. Enter SAX!

    David Megginson, principal of Magginson Technologies External link led the initiative on XML-DEV mailing list, to create a standard interface for event based XML parsing, called SAX or Simple API for XML.

    The original SAX 1.0 was released in May 1998. SAX 2.0 was released in early February last year.

    The MSXML 3.0 release offers fewer than a dozen interfaces that you can implement to create SAX applications for reading and processing XML documents. These interfaces map as closely as possible to the publicly developed, Java-based SAX2. The SAX2 parser reads an XML document and generates events based on specific symbols that it encounters in the document. The SAX2 parser does not create a tree structure in memory for the document, it processes a document's contents sequentially and generates events as it reads through the document.

    So, essentially XML Parsing using SAX involves implementing event handler classes. You only have to implement handlers for those events you wish to process. If the (Visual Basic) application must be informed of basic parsing events, it should implement IVBSAXContentHandler External link interface and registers an instance with the XML Reader (SAXXMLReader External link ) using the contentHandler property.

    Let's get started and write a tiny Visual Basic 6.0 application that uses MSXML SAX 2.0 implementation to load and parse an XML document. We'll continue to use our BankAccount sample XML document and display Balance in the following example.

    Once again, create a new Visual Basic 6.0 Standard EXE project, add reference to MSXML 3.0, and add a new class module (Project | Add Class Module). Copy and paste (or write) following code in this new class file:

    Option Explicit
    Dim bFlag As Boolean

    Implements IVBSAXContentHandler

    Private Sub IVBSAXContentHandler_startElement(strNamespaceURI As String, strLocalName As String, strQName As String, ByVal attributes As MSXML2.IVBSAXAttributes)
       If strLocalName = "Balance" Then
           bFlag = True
       Else
           bFlag = False
       End If
    End Sub

    Private Sub IVBSAXContentHandler_endElement(strNamespaceURI As String, strLocalName As String, strQName As String)
       If strLocalName = "Balance" Then
          bFlag = False
       End If
    End Sub

    Private Sub IVBSAXContentHandler_characters(text As String)
       If bFlag = True Then
          MsgBox "Your Balance is: $" & text
       End If
    End Sub

    Private Property Set IVBSAXContentHandler_documentLocator(ByVal RHS As MSXML2.IVBSAXLocator)
    End Property

    Private Sub IVBSAXContentHandler_endDocument()
    End Sub

    Private Sub IVBSAXContentHandler_endPrefixMapping(strPrefix As String)
    End Sub

    Private Sub IVBSAXContentHandler_ignorableWhitespace(strChars As String)
    End Sub

    Private Sub IVBSAXContentHandler_processingInstruction(target As String, data As String)
    End Sub

    Private Sub IVBSAXContentHandler_skippedEntity(strName As String)
    End Sub

    Private Sub IVBSAXContentHandler_startDocument()
    End Sub

    Private Sub IVBSAXContentHandler_startPrefixMapping(strPrefix As String, strURI As String)
    End Sub



    The above class code implements IVBSAXContentHandler interface methods. We set the flag to TRUE when we encounter the Balance node and MsgBox the text when the immediate characters method is called.

    Let's now look at the code which actually initiates the SAX XML parsing and see how it calls methods in our above class that implements IVBSAXContentHandler.

    Double click on the form and add following code under the Form_Load() method:

    'Create the SAXXMLReader object
    Dim objSAXReader As New SAXXMLReader

    'Create the Event Handler object
    Dim objHandler As New Class1

    'Set our hander as the sink to XML Parsing events
    Set objSAXReader.contentHandler = objHandler

    'Finally, load and do XML Parsing
    objSAXReader.parseURL "c:\1.xml"

    End

    The above code simply creates SAXXMLReader object and an instance of our event handler. Next it updates SAXXMLReader's contentHandler property and finally calls parseURL, which actually looks for the XML document, starts parsing it and calls methods in our event handler. Just step through the code (F8) and you'll notice the sequence of events.

    Related links:
    perfectxml.com DOM SAX Focus section [http://www.perfectxml.com/domsax.asp]


  • XML Data Source Object
    MSXML.dll comes with a built in control, called XMLDSOControl, that you can use for data binding. For instance, you can bind HTML elements to an XML data set. By doing so, you can display a table of elements and attributes contained in the XML data. The contents of the table will change as the XML data changes. Or you can bind an XML data set with an ADO recordset, and then use ADO objects and methods to query values from the XML data set. This control has ProgID as Msxml2.DSOControl and CLSID as {F6D90F14-9C73-11D3-B32E-00C04F990BB4}.

    In this section, we'll be learning XMLDSO with the help of two small examples. In order to run these examples, you'll need Internet Explorer 5.0 or higher and MSXML 3.0 installed. Here we go...

    Example 1: XML Data Islands
    Data Island by definition refers to "A format for putting XML-based data inside HTML pages (using <XML> or <SCRIPT language="XML">). HTML is used as the primary document or display format, and XML is used to embed data within the document..

    Save following text into c:\1.html and then open 1.html in the Internet Explorer browser.

    <html>
    <head>
    <title>XML Data Island Example</title>
    </head>
    <body>
    <XML ID="ACCOUNT">
    <?xml version="1.0"?>
    <BankAccount>
    <Number>1234</Number>
    <Name>Darshan Singh</Name>
    <Type>Checking</Type>
    <OpenDate>11/04/1974</OpenDate>
    <Balance>25382.20</Balance>
    </BankAccount>
    </XML>
    <table border="1" datasrc="#ACCOUNT">
    <tr>
    <td>Account Number </td>
    <td>Name </td>
    <td>Type of Account </td>
    <td>Balance </td>
    </tr>
    <tr>
    <td><div datafld="Number"></div></td>
    <td><div datafld="Name"></div></td>
    <td><div datafld="Type"></div></td>
    <td><div datafld="Balance"></div></td>
    </tr>
    </table>
    </body>
    </html>

    Demo

    In the above HTML file, we have an inline XML data set, and then a table binds to that data set using it's datasrc attribute. You can also link to an XML document, if you don't wish to write inline XML dataset. Use following syntax to link to an XML data set:
    <XML ID="someID" src="someURL"></XML>
    Example: <XML ID="ACCOUNT" src="c:\1.xml"></XML>

    Example 2: Using XML Data Source OBJECT
    In the above example, Internet Explorer automatically binds table rows with the XML data set. But if you were to manipulate the XML document through some script on the client page, you'll have to instantiate the XMLDSO using the OBJECT tag. Here is how you do it. Save following text into an HTML file and then open that HTML file in Internet Explorer browser:
    <html>
    <head>
    <title>MSXML DSO Example</title>
    </head>
    <body>

    <OBJECT width=0 height=0 classid="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39" id="xmldso">

      <BankAccount><Number>1234</Number><Name>Darshan Singh</Name>
      <Type>Checking</Type><OpenDate>11/04/1974</OpenDate><Balance>25382.20</Balance>
      </BankAccount>

    </OBJECT>

    <SCRIPT for=window event=onload>
       var doc = xmldso.XMLDocument;
       doc.loadXML(xmldso.altHtml);
       document.write("The Root node is: <b>" + doc.documentElement.nodeName + "</b>");
    </SCRIPT>

    </body>
    </html>

    Demo

    In this example also, the XML data set is inline. If you wish to use an external data set, then do not write anything between <OBJECT> and </OBJECT> tag. And then in the script, instead of calling loadXML call load and pass it the URL for the external XML document.


In the second part of this article, we'll learn about other standards that MSXML supports, including XSLT, XPath, XDR Schemas and more. If you have any questions or comments, feel free to contact author of this article, Darshan Singh at darshan@perfectxml.com.

  

Back to Articles Page      

All information on this site is for training only. We do not warrant its correctness or its fitness to be used. The risk of using it remains entirely with the user. 

 

  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy