首先要引用xml
寫一個(gè)XML文件的方法
Dim xmldoc As New MSXML2.DOMDocument
Dim rootElement, toElement As IXMLDOMElement
Dim cEle, dEle As IXMLDOMElement
Dim toEleText, vsEleText As IXMLDOMText
xmldoc.async = False
Dim pi As IXMLDOMProcessingInstruction
Set pi = xmldoc.createProcessingInstruction("xml", "version='1.0' encoding='gb2312'")
xmldoc.insertBefore pi, xmldoc.childNodes(0)
Set rootElement = xmldoc.createElement("Meno")
Set xmldoc.documentElement = rootElement
rootElement.setAttribute "xmlns:xsd", "http://www.w3.org/2001/XMLSchema"
Dim n As Long
For n = 0 To 100
Set toElement = xmldoc.createElement("to")
rootElement.appendChild toElement
Set cEle = xmldoc.createElement("vv")
Set dEle = xmldoc.createElement("mm")
toElement.appendChild cEle
toElement.appendChild dEle
Set toEleText = xmldoc.createTextNode(n)
Set vsEleText = xmldoc.createTextNode("SCUT")
cEle.appendChild toEleText
dEle.appendChild vsEleText
Next
xmldoc.save ("e:\test1.xml")
查詢有多少個(gè)vv的值大于50
Dim xmldoc As New MSXML2.DOMDocument
xmldoc.Load "e:\test1.xml"
Dim objNodeList As MSXML2.IXMLDOMNodeList
Set objNodeList = xmldoc.selectNodes("Meno/to/vv[text()>10]")
MsgBox objNodeList.length
修改XML文件
Dim xmldoc As New MSXML2.DOMDocument
xmldoc.Load "e:\test1.xml"
Dim objNode As IXMLDOMNode
Set objNode = xmldoc.selectSingleNode("Meno/to/vv[text()=59]")
objNode.parentNode.childNodes(1).Text = "ZSU"
xmldoc.save "e:\test1.xml"
聯(lián)系客服