xml - 尝试通过 VBA 中的标记名称查询 XML 节点,可能是命名空间问题?
问题描述
我正在尝试<content>
从 XML 文档中获取节点,使用:
Private Sub test1()
Dim xmldoc As MSXML2.DOMDocument60
Dim xmlNamespace As String
Dim nodeList As MSXML2.IXMLDOMNodeList
Dim node As MSXML2.IXMLDOMNode
Dim xml As String
Set xmldoc = New MSXML2.DOMDocument60
xmlNamespace = "xmlns:d='http://schemas.microsoft.com/ado/2007/08/dataservices' xmlns='http://www.w3.org/2005/Atom'"
xmldoc.setProperty "SelectionNamespaces", xmlNamespace
xmldoc.setProperty "SelectionLanguage", "XPath"
xml = "<?xml version=""1.0""?>" & _
"<entry xml:base=""https://example.com/Sites/xyz/_api/"" xmlns=""http://www.w3.org/2005/Atom"" xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"" xmlns:georss=""http://www.georss.org/georss"" xmlns:gml=""http://www.opengis.net/gml"" m:etag="""39""">" & _
"<id>Web/Lists(guid'abc')/Items(597)</id>" & _
"<category term=""SP.Data.xListItem"" scheme=""http://schemas.microsoft.com/ado/2007/08/dataservices/scheme""/>" & _
"<link rel=""edit"" href=""Web/Lists(guid'abc')/Items(597)""/>" & _
"<title/>" & _
"<updated>2018-07-28T02:06:34Z</updated>" & _
"<author>" & _
"<name/>" & _
"</author>" & _
"<content type=""application/xml"">" & _
"<m:properties>" & _
"<d:Id m:type=""Edm.Int32"">597</d:Id>" & _
"<d:xId m:type=""Edm.Int32"">59</d:xId>" & _
"<d:x2Id m:type=""Edm.Int32"">0</d:x2Id>" & _
"<d:ID m:type=""Edm.Int32"">597</d:ID>" & _
"</m:properties>" & _
"</content>" & _
"</entry>"
xmldoc.LoadXML xml
Set node = xmldoc.SelectSingleNode("//content")
End Sub
无论我使用“内容”还是“//内容”,该node
值始终设置为。nothing
我认为一定存在命名空间问题,因为我能够成功查询Id
:
xmldoc.SelectSingleNode("//d:Id")
那么如何查询标签名称在默认命名空间中的节点呢?
更新:QHarr 的回答是正确的,但是如果我想同时查询默认命名空间和另一个命名空间,我需要设置多个命名空间,如下所示:
xmlNamespace = "xmlns:d='schemas.microsoft.com/ado/2007/08/dataservices' xmlns:content='w3.org/2005/Atom'"
解决方案
在顶部添加命名空间
xmldoc.setProperty "SelectionNamespaces", "xmlns:content=""http://www.w3.org/2005/Atom"""
代码:
Option Explicit
Private Sub test1()
Dim xmldoc As MSXML2.DOMDocument60
Dim xmlNamespace As String
Dim nodeList As MSXML2.IXMLDOMNodeList
Dim node As MSXML2.IXMLDOMNode
Dim xml As String
Set xmldoc = New MSXML2.DOMDocument60
xmlNamespace = "xmlns:d='http://schemas.microsoft.com/ado/2007/08/dataservices' xmlns='http://www.w3.org/2005/Atom'"
xmldoc.setProperty "SelectionNamespaces", xmlNamespace
xmldoc.setProperty "SelectionLanguage", "XPath"
xmldoc.setProperty "SelectionNamespaces", "xmlns:content=""http://www.w3.org/2005/Atom"""
xml = "<?xml version=""1.0""?>" & _
"<entry xml:base=""https://example.com/Sites/xyz/_api/"" xmlns=""http://www.w3.org/2005/Atom"" xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"" xmlns:georss=""http://www.georss.org/georss"" xmlns:gml=""http://www.opengis.net/gml"" m:etag="""39""">" & _
"<id>Web/Lists(guid'abc')/Items(597)</id>" & _
"<category term=""SP.Data.xListItem"" scheme=""http://schemas.microsoft.com/ado/2007/08/dataservices/scheme""/>" & _
"<link rel=""edit"" href=""Web/Lists(guid'abc')/Items(597)""/>" & _
"<title/>" & _
"<updated>2018-07-28T02:06:34Z</updated>" & _
"<author>" & _
"<name/>" & _
"</author>" & _
"<content type=""application/xml"">" & _
"<m:properties>" & _
"<d:Id m:type=""Edm.Int32"">597</d:Id>" & _
"<d:xId m:type=""Edm.Int32"">59</d:xId>" & _
"<d:x2Id m:type=""Edm.Int32"">0</d:x2Id>" & _
"<d:ID m:type=""Edm.Int32"">597</d:ID>" & _
"</m:properties>" & _
"</content>" & _
"</entry>"
If Not xmldoc.LoadXML(xml) Then
Err.Raise xmldoc.parseError.ErrorCode, , xmldoc.parseError.reason
Exit Sub
End If
Set node = xmldoc.SelectSingleNode("//content:content")
Debug.Print node.Text
End Sub
推荐阅读
- tfs - 使用 git-tfs 将 GIT 克隆到 TFS (AzureDevOps)
- asp.net - 处理损坏的数据库关系的业务层
- reactjs - 通过自定义 npm 包反应组件。找不到模块
- linux - 在括号前的每行末尾添加数字
- excel - 选择选项按钮时解锁组合框
- elasticsearch - 在 Macbook 上本地运行 ElasticSearch
- sql-server - 在 where 条件中使用 IN 子句的索引
- reactjs - 使用 onSelect 函数调用更新 react-final-form Field 组件的值?
- import - Prestashop - 为颜色属性导入纹理图像
- r - 将数据框单列中的值向上移动,并在另一列中指定滞后