简体   繁体   中英

Trying to get a value in an XML document at a given namespace and name using MSXML

I have an XML document that looks like this:

<?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="&quot;5&quot;">
<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>

I want to get the value at the <d:Title> node, which in this case is item 2 has a new value.

When I use the following:

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

The SelectSingleNode() call throws this error:

Reference to undeclared namespace prefix: 'd'.

This also happens if I try to first set node with m as follows:

Set node = domDoc.SelectSingleNode("m:properties")

How can I get at the inner text of <d:Title> here?

在调用SelectSingleNode()之前,我需要设置DOM文档的SelectionNamespaces属性,如下所示:

domDoc.SetProperty "SelectionNamespaces", "xmlns:d='http://schemas.microsoft.com/ado/2007/08/dataservices'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM