繁体   English   中英

使用 vba 从 XML 文件中提取数据到 MS excel

[英]Extract data from XML file to MS excel using vba

Hi i have an xml response from an API, i want to extract the data from xml file to excel, but all the fields are in section. 我是 XML 的初学者,我无法从部分中提取数据。 我试图编写一些代码,但没有一个成功。

我写了一些代码,但它不起作用。

您需要的数据是嵌入在响应 XML 的 CDATA 部分中的单独 XML 文档。

您需要提取 XML 并将其加载到第二个文档 object 中,以便您可以提取所需的数据。

例如:

    Sub Test()
        
        'Tools->References->Microsoft Xml v.60
        Dim dom As MSXML2.DOMDocument60, childDoc As MSXML2.DOMDocument60
        Dim recXML As String, rec, recs
        
        Set dom = New MSXML2.DOMDocument60
        Set childDoc = New MSXML2.DOMDocument60
        
        dom.Load "C:\Users\Tim\OneDrive\Desktop\temp\response.xml"
        
        'you need to set up the namespace property
        dom.SetProperty "SelectionNamespaces", "xmlns:abc='http://www.isinet.com/xrpc42'"
        
        Dim xmlSomeCData As MSXML2.IXMLDOMElement
        'use the namspace alias we created earlier when querying
        Set xmlSomeCData = dom.SelectSingleNode("abc:response/abc:map/abc:map/abc:val")
    
        recXML = xmlSomeCData.Text '<<< the text in the CDATA section is the source XML
                                   '    to be loaded into ChildDoc
        childDoc.LoadXML recXML
        'select some nodes from the embedded xml document....
        Set recs = childDoc.SelectNodes("//records/REC/UID")
        Debug.Print recs.Length
        For Each rec In recs
            Debug.Print rec.Text
        Next rec
    
    End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM