首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试在VBA中通过标记名查询XML节点,可能是名称空间问题?

尝试在VBA中通过标记名查询XML节点,可能是名称空间问题?
EN

Stack Overflow用户
提问于 2018-07-28 10:29:49
回答 1查看 271关注 0票数 1

我正在尝试从一个XML文档中获取<content>节点,使用:

代码语言:javascript
运行
复制
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=""&quot;39&quot;"">" & _
    "<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

无论我使用的是"content“还是”// node“,content值总是设置为nothing。我认为一定是名称空间有问题,因为我可以使用以下命令成功查询Id

代码语言:javascript
运行
复制
xmldoc.SelectSingleNode("//d:Id")

那么如何查询标记名在默认名称空间中的节点呢?

更新:QHarr的答案是正确的,但如果我想查询默认名称空间和另一个名称空间,我需要设置多个名称空间,如下所示:

代码语言:javascript
运行
复制
xmlNamespace = "xmlns:d='schemas.microsoft.com/ado/2007/08/dataservices' xmlns:content='w3.org/2005/Atom'"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-28 13:54:36

在顶部添加名称空间

代码语言:javascript
运行
复制
xmldoc.setProperty "SelectionNamespaces", "xmlns:content=""http://www.w3.org/2005/Atom"""

代码:

代码语言:javascript
运行
复制
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=""&quot;39&quot;"">" & _
          "<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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51567540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档