excel - 在 Excel 中使用 Xpath 读取子节点的属性
问题描述
我在从 source_lvl2 子节点获取“数据导入”的值时遇到问题。在 Excel 中,我收到 91 的运行时错误,“对象变量或未设置块变量”
我看不出我做错了什么——有什么建议吗?
VBA
Sub TestXML()
Dim XDoc As Object
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.Load ("C:\171215-000438_1513346972.xml")
Set lists = XDoc.SelectNodes("/archive/primary_rnw_contact/source/source_lvl2")
Debug.Print lists(0).Attributes(0).Text
Set XDoc = Nothing
End Sub
XML
额外代码
<?xml version="1.1" encoding="UTF-8"?>
<archive product="RightNow" version="4.0" build="17.8.0.1.0.248" label="Archived Incident">
解决方案
您可以尝试以下(未测试):
Dim xmap As XmlMap
Dim k as Long
Dim oMyNewColumn As ListColumn
oMyList As ListObject
' Delete all previous XML maps
k = ThisWorkbook.XmlMaps.Count 'Detect all XML maps
For i = 1 To k
ThisWorkbook.XmlMaps(i).Delete
Next i
...
Set xmap = ThisWorkbook.XmlMaps.Add("some_file.xml")
...
Set oMyNewColumn = oMyList.ListColumns.Add
oMyList.ListColumns(3).XPath.SetValue xmap, "/archive/primary_rnw_contact/source/source_lvl2"
推荐阅读
- javascript - 当扩展一个类并使用原始类的属性时,你在 super() 里面放了什么
- wordpress - Wordpress 网站未在 IE 中加载(Firefox 和 Chrome 都可以)
- dia - 如何更改 Dia 中的字体并修复其字体渲染问题?
- macos - 通过命令行将文件 URL 和参数发送到(正在运行的)macOS 应用程序
- javascript - Unhandle Promise sequelize hooks 和加密密码
- ansible - 使用 ansible 在循环中将主机变量写入文件
- python - 如何创建和访问模板函数的动态变量名称?
- javascript - 将“浮点数字符串”拆分为其整数和小数部分的最安全方法是什么
- javascript - 如何在不更改 javascript 中的 URL 的情况下重新加载当前页面一次?
- python - 从 CustomUserModel 表单接收完整性错误