2017-09-20 58 views
0

我有一個看起來像這樣的XML文檔給定的名稱空間和名稱獲得一個XML文檔中的值:試圖在使用MSXML

<?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> 

我想在<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>內的文字?

回答

0

我需要調用SelectSingleNode()之前設置DOM文檔的​​屬性,如下所示:

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