现在继续我在上一系列中介绍的例子。我们将实现一个简单的搜索机制,
也许你的兴趣不是在获取书籍的标题而是在要获得整个书节点(note)上。
那么使用下面这个函数就能够满足你的要求了
Public Function GetFilteredElements(activeElement As Variant, RegExpfilter As String, _
Optional queryString As String = "",optional IsGlobal as Boolean=True, _
optional IgnoreCase as Boolean = True) As IXMLDOMNodeList
Dim re As RegExp
Dim filterDoc As DOMDocument
Dim nodelist As IXMLDOMNodeList
Dim filterList As IXMLDOMNodeList
Dim node As IXMLDOMElement
Dim baseNode As IXMLDOMElement
Set re = New RegExp
On Error GoTo ErrorHandler
Select Case TypeName(activeElement)
Case "IXMLDOMElement"
Set baseNode = activeElement
Case "DOMDocument"
Set baseNode = activeElement.documentElement
Case Else
Error 1001
Set GetFilteredElements = Nothing
End Select
re.Pattern = RegExpfilter
re.Global=IsGlobal
re.IgnoreCase=IgnoreCase
If queryString = "" Then
Set filterList = baseNode.selectNodes(".[textnode()]|.//*[textnode()]")
Else
Set filterList = baseNode.selectNodes(queryString)
End If
For Each node In filterList
If re.Test(node.Text) Then
node.setAttribute "filter:filteredElementFound", "true"
End If
Next
Set filterList = baseNode.selectNodes(".[@filter:filteredElementFound]|.//*[@filter:filteredElementFound]")
For Each node In filterList
node.removeAttribute "filter:filteredElementFound"
Next
Set GetFilteredElements = filterList
Exit Function
ErrorHandler:
If Err.Number = 1001 Then
MsgBox "Document must be an XML document, or a document element."
Else
Error Err.Number
End If
End Function
下面是对这个方法的简单描述:
GetFilteredElements主要是用来获取一个XML的文档或则一个文档里面的节点,并且