尝试使用 MSXML 在给定命名空间和名称的 XML 文档中获取值
Trying to get a value in an XML document at a given namespace and name using MSXML
我有一个 XML 文档,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<entry xml:base="https://sps.utility.abc123.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=""5"">
<id>Web/Lists(guid'c920cb93-a31a-49a0-a4d6-ac1cb8fb0658')/Items(2)</id>
<category term="SP.Data.REST_x0020_Test_x0020_ListListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="Web/Lists(guid'c920cb93-a31a-49a0-a4d6-ac1cb8fb0658')/Items(2)" />
<title />
<updated>2017-09-20T16:01:19Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Title>item 2 has a new value.</d:Title>
</m:properties>
</content>
</entry>
我想获取 <d:Title>
节点的值,在本例中为 item 2 has a new value.
当我使用以下内容时:
Private Function getFieldValueFromXml(xml As Variant, fieldName As String)
Dim node As IXMLDOMNode
Dim namespaceAndName As String
Dim domDoc as MSXML2.DomDocument60
namespaceAndName = "d:" & fieldName
Set domDoc = New DOMDocument60
Set node = domDoc.SelectSingleNode(namespaceAndName)
getFieldValueFromXML = node.Text
End Function
SelectSingleNode()
调用抛出此错误:
Reference to undeclared namespace prefix: 'd'.
如果我尝试首先使用 m
设置节点,也会发生这种情况,如下所示:
Set node = domDoc.SelectSingleNode("m:properties")
如何在此处获取 <d:Title>
的内部文本?
我需要在调用SelectSingleNode()
之前设置DOM文档的SelectionNamespaces
属性,如下:
domDoc.SetProperty "SelectionNamespaces", "xmlns:d='http://schemas.microsoft.com/ado/2007/08/dataservices'"
我有一个 XML 文档,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<entry xml:base="https://sps.utility.abc123.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=""5"">
<id>Web/Lists(guid'c920cb93-a31a-49a0-a4d6-ac1cb8fb0658')/Items(2)</id>
<category term="SP.Data.REST_x0020_Test_x0020_ListListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="Web/Lists(guid'c920cb93-a31a-49a0-a4d6-ac1cb8fb0658')/Items(2)" />
<title />
<updated>2017-09-20T16:01:19Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Title>item 2 has a new value.</d:Title>
</m:properties>
</content>
</entry>
我想获取 <d:Title>
节点的值,在本例中为 item 2 has a new value.
当我使用以下内容时:
Private Function getFieldValueFromXml(xml As Variant, fieldName As String)
Dim node As IXMLDOMNode
Dim namespaceAndName As String
Dim domDoc as MSXML2.DomDocument60
namespaceAndName = "d:" & fieldName
Set domDoc = New DOMDocument60
Set node = domDoc.SelectSingleNode(namespaceAndName)
getFieldValueFromXML = node.Text
End Function
SelectSingleNode()
调用抛出此错误:
Reference to undeclared namespace prefix: 'd'.
如果我尝试首先使用 m
设置节点,也会发生这种情况,如下所示:
Set node = domDoc.SelectSingleNode("m:properties")
如何在此处获取 <d:Title>
的内部文本?
我需要在调用SelectSingleNode()
之前设置DOM文档的SelectionNamespaces
属性,如下:
domDoc.SetProperty "SelectionNamespaces", "xmlns:d='http://schemas.microsoft.com/ado/2007/08/dataservices'"