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

前端之家收集整理的这篇文章主要介绍了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

猜你在找的VB相关文章