2011-10-20 156 views
3

如何編寫vbscript應該搜索XML文件中的特定節點並將該節點的值替換爲另一個值?VBScript在XML節點中查找節點並替換值

到目前爲止,我可以讀取節點並獲取值。

set objXML = CreateObject("Microsoft.XMLDOM") 
objXML.async = "false" 
objXML.load("E:\sage2\test.xml") 
Set Root = objXML.documentElement 

For Each x In Root.childNodes 

if x.nodename="showList" then 
    plot=x.text 
    msgbox plot 
end if 
Next 

請建議我一些例子,它應該讀取xml文件中的特定節點並替換該節點的值。

回答

8

這是VBScript中的一個簡單的XML編輯和保存示例。我推薦使用Xpath來選擇你的節點,而不是循環遍歷子節點,你可以提供你的XML來獲得更詳細的答案。

Set xmlDoc = CreateObject("Microsoft.XMLDOM") 
xmlDoc.load "MYFILE.xml" 

'Locate the desired node 
'Note the use of XPATH instead of looping over all the child nodes 
Set nNode = xmlDoc.selectsinglenode ("//parentnode/targetnode") 

'Set the node text with the new value 
nNode.text = "NEW VALUE" 

'Save the xml document with the new settings. 
strResult = xmldoc.save("MYFILE.xml") 
+2

-0.5使用.save方法(一個普通的Sub)作爲返回字符串的函數。 –