1

我是 VBA 和 XML 的新手,非常感谢您的帮助。我有以下代码:

Option Explicit
Sub XMLfromPPTExample()
Dim XDoc As MSXML2.DOMDocument
Dim objxmlnodexbrl As MSXML2.IXMLDOMNode
Dim objXMLNodeDIIRSP As MSXML2.IXMLDOMNode
Set XDoc = New MSXML2.DOMDocument
XDoc.async = False
XDoc.validateOnParse = False
XDoc.Load("https://www.sec.gov/Archives/edgar/data/936468/000119312516476010/lmt-20151231.xml")
Set objxmlnodexbrl = XDoc.SelectSingleNode("xbrl")
Set objXMLNodeDIIRSP = objxmlnodexbrl.SelectSingleNode("us-gaap:GrossProfit")
Worksheets("Tabelle1").Range("A1").Value = objXMLNodeDIIRSP.Text
End Sub

这将访问 SEC 的 Edgar 数据库中的特定 XML 表,并将定义的标签(“us-gaap:GrossProfit”)的值写入 Excel 字段 A1。

但是,此标记可能在此 XML 文件中以不同的值出现多次。我需要的是这些事件中的每一个都被打印到一个excel表中,包括“contextRef”的值和值。

您能否修改我的代码以使其正常工作?非常感谢。

4

1 回答 1

1

考虑使用 MSXML DOM 的XPath,确保声明命名空间us:gaap[#]下面按每个<us-gaap:grossProfit>标签的节点索引进行迭代:

Sub XMLfromPPTExample()
On Error GoTo ErrHandle
    Dim XDoc As MSXML2.DOMDocument
    Dim objxmlnodexbrl As MSXML2.IXMLDOMNode
    Dim grossProfitList As MSXML2.IXMLDOMNodeList
    Dim XmlNamespaces As String
    Dim row As Integer

    Set XDoc = New MSXML2.DOMDocument
    XDoc.async = False
    XDoc.validateOnParse = False
    XDoc.Load ("https://www.sec.gov/Archives/edgar/data/936468/000119312516476010/lmt-20151231.xml")

    XmlNamespaces = "xmlns:us-gaap='http://fasb.org/us-gaap/2015-01-31'"
    XDoc.setProperty "SelectionNamespaces", XmlNamespaces
    XDoc.setProperty "SelectionLanguage", "XPath"

    Set grossProfitList = XDoc.DocumentElement.SelectNodes("//us-gaap:GrossProfit")

    For row = 1 To grossProfitList.Length
        Worksheets("Tabelle1").Range("A" & row).Value = XDoc.DocumentElement.SelectNodes("//us-gaap:GrossProfit[" & row & "]")(0).Text
        Worksheets("Tabelle1").Range("B" & row).Value = XDoc.DocumentElement.SelectNodes("//us-gaap:GrossProfit[" & row & "]/@contextRef")(0).Text
    Next row

    Set grossProfitList = Nothing
    Set XDoc = Nothing
    Exit Sub

ErrHandle:
    MsgBox Err.Number & " - " & Err.Description
    Exit Sub
End Sub

输出

4187000000  eol_PE11373---1510-K0017_STD_365_20131231_0
5255000000  eol_PE11373---1510-K0017_STD_365_20141231_0
5200000000  eol_PE11373---1510-K0017_STD_365_20151231_0
于 2016-09-28T00:57:46.030 回答