VB处理XML(忽略节点大小写)

'引入Microsoft XML v4.0
'
'有如下示例:

'
'一般处理:
'把得到的字符串全部转为大写,处理这样可以得到。但是里面的值也变成大写了。Command1_Click
'可以得到Code的集合,但这种方法会把节点里面的值也处理为大写,如果对里面的内容业务上大小写敏感的话就不适用了。

'用Command2_Click可以解决这个问题
Private Sub Command1_Click()
        
    Dim objDomDocument As DOMDocument

    Set objDomDocument = New DOMDocument

    '因传回来不知道所有字母的大小写所以如下处理

    objDomDocument.LoadXml UCase(getString)

    Dim xmlNodeList As IXMLDOMNodeList

    Set xmlNodeList = objDomDocument.selectNodes(UCase("Summary/CodeList/Code"))
    
    Debug.Print xmlNodeList.length
End Sub

Private Function getString() As String
    
    getString = "<Summary>" & "<Codelist>" & "<Code></Code>" & "<Code></Code>" & "<Code></Code>" & "</Codelist>" & "</Summary>"

End Function

Private Sub Command2_Click()
    
    '下面的方法可以解决
    Dim objDomDocument As DOMDocument

    Set objDomDocument = New DOMDocument
    
    objDomDocument.LoadXml getString
        
    objDomDocument.SetProperty "SelectionLanguage","XPath"

    Dim xmlNodeList As IXMLDOMNodeList

    '用下面的语句在不知道节点大小写的情况下得到想要的节点
   
    '处理一下上面的这句代码,转为

    Set xmlNodeList = objDomDocument.selectNodes(TransToXML4("Summary","CodeList","Code"))
    
    Debug.Print xmlNodeList.length
End Sub

Public Function TransToXML4(strNodeLevel1 As String,_
                            Optional strNodeLevel2 As String,_
                            Optional strNodeLevel3 As String,_
                            Optional strNodeLevel4 As String) As String
     
    Dim strTemp As String
    
        strTemp = "*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = '" & LCase$(strNodeLevel1) & "']"
        
        If Len(strNodeLevel2) > 0 Then
            strTemp = strTemp & "/*[translate(name(),'abcdefghijklmnopqrstuvwxyz') = '" & LCase$(strNodeLevel2) & "']"
        End If

        If Len(strNodeLevel3) > 0 Then
        
            strTemp = strTemp & "/*[translate(name(),'abcdefghijklmnopqrstuvwxyz') = '" & LCase$(strNodeLevel3) & "']"
        End If
        
        If Len(strNodeLevel4) > 0 Then
        
            strTemp = strTemp & "/*[translate(name(),'abcdefghijklmnopqrstuvwxyz') = '" & LCase$(strNodeLevel4) & "']"
        End If
        
        TransToXML4 = strTemp
    

    ' strRecordDate = objDomDocument.selectSingleNode("*[translate(name(),'abcdefghijklmnopqrstuvwxyz') = 'salesman']/*[translate(name(),'abcdefghijklmnopqrstuvwxyz') = 'recorddate']").Text

End Function

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 --------------------------...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, t...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integ...