在VBA中解析XML

vic*_*tor 3 xml vba

我有一个XML ResponseXML对象.我想循环遍历所有称为"XYZ"的节点.我该怎么做呢?

ArB*_*rBR 11

下面是一些你可以使用函数解析您的XML:

Private xml As MSXML.DOMDocument

Private Sub loadXMLFile(xmlFile)    
    Set xml = New DOMDocument
    xml.async = False
    xml.Load (xmlFile) 
End Sub

Private Sub loadXMLString(xmlString)    
    Set xml = New DOMDocument
    xml.LoadXml (xmlString) 
End Sub

Public Function getNodeValue(xpath As String) As String    
    getNodeValue = xml.SelectSingleNode(strXPath).Text    
End Function

Public Function getNodes(xpath as string) As IXMLDOMNodeList            
    Set getNodes = xml.SelectNodes(xpath)
End Function

Public Function getNode(xpath as string) As IXMLDOMNode
    Set getNode = xml.SelectSingleNode(xpath)
End Function
Run Code Online (Sandbox Code Playgroud)

有关MSXML的更多信息,请参阅MSDN:http://msdn.microsoft.com/en-us/library/aa468547.aspx

  • 值得记住`DOMDocument`是`DOMDocument30`(即MXSML v3.0)的同义词,并且任何更新的版本必须具有在`DOMDocument60`中指定的完整版本(对于MSXML v6.0) - 请参阅http:/ /msdn.microsoft.com/en-us/library/ms757837%28VS.85%29.aspx和http://blogs.msdn.com/b/xmlteam/archive/2006/10/23/using-the-right -version-的-MSXML功能于互联网explorer.aspx (4认同)