VBA XML 子节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21683064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:50:48  来源:igfitidea点击:

VBA XML ChildNodes

xmlexcelvbaexcel-vba

提问by ExoticBirdsMerchant

I am trying to modify a fine piece of code that i got from user2140261 that pulls data from the XML type (XBRL-Template) SEC EDGAR Database. So since i want to pull element values from other Nodes as well, my first idea was to show the list with the Nodes of the XML instance in a MsgBox; by using the childNodes Property. The first little snippet, has the minor modification I made to avoid any confusion while in the second one we see the original code plus the 3 statements of the modifications

我正在尝试修改我从 user2140261 获得的一段很好的代码,它从 XML 类型(XBRL 模板)SEC EDGAR 数据库中提取数据。因此,由于我也想从其他节点中提取元素值,因此我的第一个想法是在 MsgBox 中显示带有 XML 实例的节点的列表;通过使用childNodes 属性。第一个小片段,我做了一些小修改以避免任何混淆,而在第二个小片段中,我们看到原始代码加上修改的 3 条语句

Dim Tiger As String  

Tiger = objXMLNodexbrl.ChildNodes(1)

MsgBox Tiger


Sub GetNode()
Dim strXMLSite As String
Dim objXMLHTTP As MSXML2.XMLHTTP
Dim objXMLDoc As MSXML2.DOMDocument
Dim objXMLNodexbrl As MSXML2.IXMLDOMNode
Dim objXMLNodeDIIRSP As MSXML2.IXMLDOMNode
Dim Tiger As String

Set objXMLHTTP = New MSXML2.XMLHTTP
Set objXMLDoc = New MSXML2.DOMDocument

strXMLSite = "http://www.sec.gov/Archives/edgar/data/10795/000119312513456802/bdx-20130930.xml"

objXMLHTTP.Open "POST", strXMLSite, False
objXMLHTTP.send
objXMLDoc.LoadXML (objXMLHTTP.responseText)

Set objXMLNodexbrl = objXMLDoc.SelectSingleNode("xbrl")

Tiger = objXMLNodexbrl.ChildNodes(1)

MsgBox Tiger

Set objXMLNodeDIIRSP = objXMLNodexbrl.SelectSingleNode("us-gaap:DebtInstrumentInterestRateStatedPercentage")

Worksheets("Sheet1").Range("A1").Value = objXMLNodeDIIRSP.Text
End Sub


So when i am trying to parse step-by-step the code using an F8 i get a 'Runtime Error 438' telling me that the object doesn't support this property or method.

因此,当我尝试使用 F8 逐步解析代码时,我收到“运行时错误 438”,告诉我该对象不支持此属性或方法。

enter image description here

在此处输入图片说明

  • How is that possible when xbrlin the instance document is the root element and it contains all the child nodes?
  • 当实例文档中的xbrl是根元素并且它包含所有子节点时,这怎么可能?

@user2140261 wow you were so right...just one question i do change my code with

@user2140261 哇,你说得对……只有一个问题我确实改变了我的代码

Tiger = objXMLNodexbrl.ChildNodes(1).Text

but i am getting the numerical value of the element instead of the number of the nodes. How is that possible? since ChildNodes Property : "Contains a node list containing the child nodes"...

但我得到的是元素的数值而不是节点的数量。这怎么可能?由于 ChildNodes 属性:“包含一个包含子节点的节点列表”...

enter image description here

在此处输入图片说明

回答by user2140261

Change your line of code from:

更改您的代码行:

Tiger = objXMLNodexbrl.ChildNodes(1)

To

Tiger = objXMLNodexbrl.ChildNodes(1).Text

ALTERNATIVLYyou could

或者你可以

change Tiger from a string to a MSXML2.IXMLDOMNode

将 Tiger 从字符串更改为 MSXML2.IXMLDOMNode

then change

然后改变

MsgBox Tiger

To

MsgBox Tiger.Text

objXMLNodexbrl.ChildNodes.Lengthwill return the count of nodes inside of XBRL

objXMLNodexbrl.ChildNodes.Length将返回其中的节点数 XBRL

If you are trying to get a list of all Child Node Names of XBRL then Dim another MSXML2.IXMLDOMNode called nodeCurrentand use the following:

如果您尝试获取 XBRL 的所有子节点名称的列表,则 Dim 调用另一个 MSXML2.IXMLDOMNodenodeCurrent并使用以下内容:

For Each nodeCurrent In objXMLNodexbrl.ChildNodes
    Tiger = Tiger & ", " & nodeCurrent.nodeName
Next nodeCurrent

MsgBox Tiger