Source: validateDOM.frm

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Developer's Guide

Source: validateDOM.frm

The source code performs the following basic steps:

  1. Creates a DOM instance (oXMLDoc) to hold the XML data.
  2. Creates a DOM instance (oXSDDoc) to hold the XML Schema definition.
  3. Creates an IXMLSchemaCollection or IXMLSchemaCollection2 object (oSCache). This object is also called a schema cache. The application then adds the XML Schema definition (oXSDDoc) to the oSCache.
  4. Associates oSCache with the schemas property of the DOM object for the XML data (oXMLDoc).
  5. Calls the following validation methods on the DOM object for XML data (oXMLDoc):
    • Calls the validate method on oXMLDoc to validate the data set as a whole

      and/or

    • Calls the validateNode(oNode) method on oXMLDoc to validate a node object (oNode) selected from oXMLDoc.
  6. Checks the error returned from validate method and/or the validateNode(oNode) method, to determine if the specified XML data set is valid against the given XML Schema definition.

Visual Basic Source File (validateDOM.frm)

Private Sub Form_Load()
    ' Output string:
    Dim strout As String
    strout = ""
    
    ' Load an XML document into a DOM instance.
    Dim oXMLDoc As DOMDocument50
    Set oXMLDoc = DOMFromFile(App.path + "\books.xml")
    If oXMLDoc Is Nothing Then
        Exit Sub
    End If
    
    ' Load the schema for the xml document.
    Dim oXSDDoc As DOMDocument50
    Set oXSDDoc = DOMFromFile(App.path + "\books.xsd")
    If oXSDDoc Is Nothing Then
        Exit Sub
    End If
    
    ' Create a schema cache instance.
    Dim oSCache As New XMLSchemaCache50
    
    ' Add the just-loaded schema definition to the schema collection
    oSCache.Add "urn:books", oXSDDoc
    
    ' Assign the schema to the XML document's schema collection.
    Set oXMLDoc.schemas = oSCache
    
    ' Validate the entire DOM.
    strout = strout _
        + "Validating DOM..." + vbNewLine
    Dim oError As IXMLDOMParseError
    Set oError = oXMLDoc.Validate
    If oError.errorCode <> 0 Then
        strout = strout + vbTab _
            + "XMLDoc is not valid because " _
            + vbNewLine + oError.reason + vbNewLine
    Else
        strout = strout _
            + vbTab + "XMLDoc is validated:" + vbNewLine _
            + oXMLDoc.xml + vbNewLine
    End If
    
    Dim oNodes As IXMLDOMNodeList
    ' Validate all "//books" nodes, node by node.
    strout = strout _
           + "Validating all book nodes, '//book\', " _
           + "one by one ..." + vbNewLine
    Set oNodes = oXMLDoc.selectNodes("//book")
    strout = strout + ValidateNodes(oXMLDoc, oNodes)
    
    ' Validate all children of //books nodes, node by node.
    strout = strout _
           + "Validating all children of all book nodes, //book/*, " _
           + "one by one ..." + vbNewLine
    Set oNodes = oXMLDoc.selectNodes("//book/*")
    strout = strout + ValidateNodes(oXMLDoc, oNodes)
    
    MsgBox strout
End Sub

Private Function DOMFromFile(ByVal path As String)
    If path = "" Then
        Set DOMFromFile = Nothing
        Exit Function
    End If
    
    Dim dom As New DOMDocument50
    dom.async = False
    dom.validateOnParse = False
    dom.resolveExternals = False
    dom.preserveWhiteSpace = True
    If dom.Load(path) = False Then
        MsgBox "Can't create DOM from " + path
        Set DOMFromFile = Nothing
        Exit Function
    End If
    Set DOMFromFile = dom
End Function

Private Function ValidateNodes(oXMLDoc As DOMDocument50, _
                            oNodes As IXMLDOMNodeList) As String
    If oXMLDoc Is Nothing Then
        ValidateNodes = "Error in ValidateNodes(): Invalid oXMLDoc"
        Exit Function
    End If
    
    If oNodes Is Nothing Then
        ValidateNodes = "Error in ValidateNodes(): Invalid oNodes"
        Exit Function
    End If
    
    Dim oNode As IXMLDOMNode
    Dim oError As IXMLDOMParseError
    Dim strout As String
    
    For i = 0 To oNodes.length - 1
        Set oNode = oNodes.nextNode
        If Not (oNode Is Nothing) Then
           Set oError = oXMLDoc.validateNode(oNode)
           If oError.errorCode = 0 Then
               strout = strout + vbTab _
                   + "<" + oNode.nodeName + "> (" _
                   + CStr(i) + ") is a valid node " + vbNewLine
           Else
               strout = strout + vbTab _
                   + "<" + oNode.nodeName + "> (" + CStr(i) + ") " _
                   + "is not valid because" + vbNewLine _
                   + oError.reason + vbNewLine
           End If
        End If
    Next
    ValidateNodes = strout
End Function

To add validateDOM.frm to the project

  • Copy the code listing above. Paste it into the Visual Basic code editor as the form_load subroutine, replacing any code fragments that are already there.

Next, we'll add the resource files.