Source: validateDOM.frm
[This sample code uses features that were first implemented in MSXML 5.0 for Microsoft Office Applications.]
The source code performs the following basic steps:
Creates a DOM instance (
oXMLDoc
) to hold the XML data.Creates a DOM instance (
oXSDDoc
) to hold the XML Schema definition.Creates an
IXMLSchemaCollection
orIXMLSchemaCollection2
object (oSCache
). This object is also called a schema cache. The application then adds the XML Schema definition (oXSDDoc
) to theoSCache
.Associates
oSCache
with theschemas
property of the DOM object for the XML data (oXMLDoc
).Calls the following validation methods on the DOM object for XML data (
oXMLDoc
):Calls the
validate
method onoXMLDoc
to validate the data set as a wholeand/or
Calls the
validateNode(oNode)
method onoXMLDoc
to validate a node object (oNode
) selected fromoXMLDoc
.
Checks the error returned from
validate
method and/or thevalidateNode(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 DOMDocument60
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 DOMDocument60
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 XMLSchemaCache60
' 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 DOMDocument60
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 DOMDocument60, _
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 will add the resource files (Resource: books.xml and books.xsd (validateDOM VB Example)).