The source code performs the following basic steps:
oXMLDoc) to hold the XML data.oXSDDoc) to hold the XML Schema definition.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.oSCache with the schemas property of the DOM object for the XML data (oXMLDoc).oXMLDoc):
validate method on oXMLDoc to validate the data set as a whole
and/or
validateNode(oNode) method on oXMLDoc to validate a node object (oNode) selected from oXMLDoc.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
Next, we'll add the resource files.