Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何从非第一个子XML VBA API获取记录_Excel_Xml_Vba_Child Nodes - Fatal编程技术网

Excel 如何从非第一个子XML VBA API获取记录

Excel 如何从非第一个子XML VBA API获取记录,excel,xml,vba,child-nodes,Excel,Xml,Vba,Child Nodes,我从世界银行得到了这样的数据 (来源:) 要使用此VBA代码,需要设置引用 microsoft winhttp服务 微软xml microsoft 2.0对象库 <wb:countries xmlns:wb="http://www.worldbank.org" page="1" pages="1" per_page="50" total="34"> <wb:country id="AFG"> <wb:iso2Code>AF</wb:iso2Code>

我从世界银行得到了这样的数据 (来源:) 要使用此VBA代码,需要设置引用 microsoft winhttp服务 微软xml microsoft 2.0对象库

<wb:countries xmlns:wb="http://www.worldbank.org" page="1" pages="1" per_page="50" total="34">
<wb:country id="AFG">
<wb:iso2Code>AF</wb:iso2Code>
<wb:name>Afghanistan</wb:name>
<wb:region id="SAS" iso2code="8S">South Asia</wb:region>
<wb:adminregion id="SAS" iso2code="8S">South Asia</wb:adminregion>
<wb:incomeLevel id="LIC" iso2code="XM">Low income</wb:incomeLevel>
<wb:lendingType id="IDX" iso2code="XI">IDA</wb:lendingType>
<wb:capitalCity>Kabul</wb:capitalCity>
<wb:longitude>69.1761</wb:longitude>
<wb:latitude>34.5228</wb:latitude>
</wb:country>
它一直工作到这里:

For Each xChild In xNode.ChildNodes

    Set obAtt1 = xChild.Attributes.getNamedItem("id")

    strVal = Trim(obAtt1.Text)

    ws.Cells(intRow, 2) = obAtt1.Text
    intRow = intRow + 1

Next xChild
它只适用于第一个孩子-获取国家代码,但我需要例如获取wb:name(全名)
如果您能给我任何提示,我将不胜感激。

以下是您想要的:

Dim xmlDoc作为新的MSXML2.DOMDocument
Dim国家/地区为MSXML2.IXMLDOMNodeList,国家/地区为MSXML2.IXMLDOMNode
“我需要这两个。。。。
xmlDoc.setProperty“SelectionLanguage”、“XPath”
xmlDoc.setProperty“SelectionNamespaces”,“xmlns:wb=”http://www.worldbank.org'"
'从本地文件加载以进行测试
如果不是xmlDoc.Load(ThisWorkbook.Path&“\country.xml”),则
MsgBox(“Blad ladowania URL”)
出口接头
如果结束
Set countries=xmlDoc.SelectNodes(//wb:country)
调试。打印国家。长度
每个国家
调试.打印“-------------------------------------”
Debug.Print“id”,country.Attributes.getNamedItem(“id”).Text
调试。打印“名称”,国家/地区。选择SingleNode(“wb:Name”)。NodeType值
调试。打印“地区”,国家。选择SingleNode(“wb:Region”)。NodeType值
等等
下一个国家

Hint:-)使用
Dim xmlDoc作为新的MSXML2.DOMDocument60
参考当前(推荐的)版本6.0,甚至不需要显式设置XPath属性,因为XPath 1.0已经完全受支持
MSXML2.DOMDocument
自动引用最后一个稳定版本3.0,有时出于兼容性原因需要;如上所示,它允许通过显式选择语言设置集成XPath。非常感谢@T.M。!
For Each xChild In xNode.ChildNodes

    Set obAtt1 = xChild.Attributes.getNamedItem("id")

    strVal = Trim(obAtt1.Text)

    ws.Cells(intRow, 2) = obAtt1.Text
    intRow = intRow + 1

Next xChild