尝试在VBA中按标记名查询XML节点,可能是命名空间问题?
我正在尝试使用以下方法从XML文档获取尝试在VBA中按标记名查询XML节点,可能是命名空间问题?,xml,vba,excel,xpath,Xml,Vba,Excel,Xpath,我正在尝试使用以下方法从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 = "x
节点:
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
那么,如何查询标记名位于默认命名空间中的节点呢
更新: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"""
代码:
选项显式
专用子测试1()
Dim xmldoc作为MSXML2.DOMDocument60
将xmlNamespace设置为字符串
Dim节点列表作为MSXML2.IXMLDOMNodeList
作为MSXML2.IXMLDOMNode的Dim节点
将xml设置为字符串
设置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=”“&_
"" & _
“网络/列表(guid'abc')/项目(597)”&_
"" & _
"" & _
"" & _
“2018-07-28T02:06:34Z”和_
"" & _
"" & _
"" & _
"" & _
"" & _
"597" & _
"59" & _
"0" & _
"597" & _
"" & _
"" & _
""
如果不是xmldoc.LoadXML(xml),那么
Err.Raise xmldoc.parseError.ErrorCode,xmldoc.parseError.reason
出口接头
如果结束
Set node=xmldoc.SelectSingleNode(//content:content)
Debug.Print node.Text
端接头
这适用于选择内容
,但是当我尝试设置node=xmldoc.SelectSingleNode(//d:xId”)
时,我得到一个错误引用了未声明的名称空间前缀:“d”。
有什么方法可以从多个名称空间中选择吗?没关系,我找到了:xmlNamespace=“xmlns:d=”xmlns:content=“”
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