1. XML文件结构如下: <?xml version="1.0" encoding="utf-8"?> <Database name="SQL Server" type="Standard Security"> <Provider>sqloledb</Provider> <DataSource>(local)</DataSource> <InitialCatalog>pubs</InitialCatalog> <UserId>sa</UserId> <Password>123456</Password> </Database> 读出<Provider>sqloledb</Provider>中"sqloledb"这个字符串 如下: Private Sub Command1_Click() Set xmlDoc = CreateObject("Msxml2.DOMDocument") xmlDoc.async = False xmlDoc.validateOnParse = False If xmlDoc.Load("c:\ToWord.xml") = False Then Set parseError = xmlDoc.parseError MsgBox "Load xml failure." & vbCrLf & parseError.reason Exit Sub End IfSet nodeList = xmlDoc.documentElement.selectSingleNode("//Database").childNodes msgbox nodeList.Item(0).textEnd Sub2. 一个读函数 readxml(fileName, itemName) as String '返回根节点下itemName 节点的值 一个写函数 writexml(fileName,itemName,itemValue) as boolean '将根节点下的itemName节点的值赋为 itemValue '如果fileName不存在,则创建一个xml文件Private Sub Command1_Click() a = readxml(App.Path & "\aa.xml", "itemName") MsgBox a b = writexml(App.Path & "\aa.xml", "itemName", "haha") MsgBox b End SubFunction readxml(fileName, itemName) As String Set xmldoc = CreateObject("Msxml2.DOMDocument") xmldoc.async = False xmldoc.validateOnParse = False If xmldoc.Load(fileName) = False Then Set parseError = xmldoc.parseError MsgBox "Load xml failure." & vbCrLf & parseError.reason Exit Function End If Set iName = xmldoc.documentElement.selectSingleNode(itemName) readxml = iName.Text Set xmldoc = Nothing Set iName = Nothing End FunctionFunction writexml(fileName, itemName, itemValue) As Boolean Set xmldoc = CreateObject("Msxml2.DOMDocument.4.0") Set objfso = CreateObject("Scripting.FileSystemObject") If Not objfso.fileexists(fileName) Then Dim pi As IXMLDOMProcessingInstruction Set pi = xmldoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""") xmldoc.insertBefore pi, xmldoc.childNodes.Item(0) Dim page As IXMLDOMElement Set page = xmldoc.createElement("root") Set xmldoc.documentElement = page xmldoc.save fileName writexml = False Exit Function End If xmldoc.async = False xmldoc.validateOnParse = False xmldoc.Load (fileName) Set iName = xmldoc.documentElement.selectSingleNode(itemName) iName.Text = itemValue xmldoc.save fileName writexml = True Set xmldoc = Nothing Set objfso = Nothing End Function
3.创建一个XML文件Set xmldoc = CreateObject("Msxml2.DOMDocument.4.0") Dim pi As IXMLDOMProcessingInstruction Set pi = xmldoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""") xmldoc.insertBefore pi, xmldoc.childNodes.Item(0)
' top node Dim process As IXMLDOMElement Set process = xmldoc.createElement("process") Set xmldoc.documentElement = process
' Dim element As IXMLDOMElement
' Set element = xmldoc.createElement("process-name") element.Text = "aaa" process.appendChild element xmldoc.Save "c:\test.xml"
不错不错,就这样就有大用处了 不过 writexml 可以少作改进:Function writexml(fileName, itemName, itemValue) As Boolean writexml = False Dim xmlDoc As MSXML2.DOMDocument40 Dim objFso As FileSystemObject Dim iName, rootNode As IXMLDOMElement Set xmlDoc = New MSXML2.DOMDocument40 Set objFso = New Scripting.FileSystemObject If Not objFso.fileexists(fileName) Then Dim pi As IXMLDOMProcessingInstruction Set pi = xmlDoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""") xmlDoc.insertBefore pi, xmlDoc.childNodes.Item(0) Dim page As IXMLDOMElement Set page = xmlDoc.createElement("Configuration") Set xmlDoc.documentElement = page xmlDoc.save fileName End If xmlDoc.async = False xmlDoc.validateOnParse = False xmlDoc.Load (fileName) Set iName = xmlDoc.documentElement.selectSingleNode(itemName) If iName Is Nothing Then Set iName = xmlDoc.createElement(itemName) Set rootNode = xmlDoc.selectSingleNode("Configuration") rootNode.appendChild iName End If iName.Text = itemValue xmlDoc.save fileName writexml = True Set xmlDoc = Nothing Set objFso = Nothing End Function不知这样是否更加好用一点
XML文件结构如下:
<?xml version="1.0" encoding="utf-8"?>
<Database name="SQL Server" type="Standard Security">
<Provider>sqloledb</Provider>
<DataSource>(local)</DataSource>
<InitialCatalog>pubs</InitialCatalog>
<UserId>sa</UserId>
<Password>123456</Password>
</Database>
读出<Provider>sqloledb</Provider>中"sqloledb"这个字符串
如下:
Private Sub Command1_Click()
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.async = False
xmlDoc.validateOnParse = False
If xmlDoc.Load("c:\ToWord.xml") = False Then
Set parseError = xmlDoc.parseError
MsgBox "Load xml failure." & vbCrLf & parseError.reason
Exit Sub
End IfSet nodeList = xmlDoc.documentElement.selectSingleNode("//Database").childNodes
msgbox nodeList.Item(0).textEnd Sub2.
一个读函数
readxml(fileName, itemName) as String
'返回根节点下itemName 节点的值
一个写函数
writexml(fileName,itemName,itemValue) as boolean
'将根节点下的itemName节点的值赋为 itemValue
'如果fileName不存在,则创建一个xml文件Private Sub Command1_Click()
a = readxml(App.Path & "\aa.xml", "itemName")
MsgBox a
b = writexml(App.Path & "\aa.xml", "itemName", "haha")
MsgBox b
End SubFunction readxml(fileName, itemName) As String
Set xmldoc = CreateObject("Msxml2.DOMDocument")
xmldoc.async = False
xmldoc.validateOnParse = False
If xmldoc.Load(fileName) = False Then
Set parseError = xmldoc.parseError
MsgBox "Load xml failure." & vbCrLf & parseError.reason
Exit Function
End If
Set iName = xmldoc.documentElement.selectSingleNode(itemName)
readxml = iName.Text
Set xmldoc = Nothing
Set iName = Nothing
End FunctionFunction writexml(fileName, itemName, itemValue) As Boolean
Set xmldoc = CreateObject("Msxml2.DOMDocument.4.0")
Set objfso = CreateObject("Scripting.FileSystemObject")
If Not objfso.fileexists(fileName) Then
Dim pi As IXMLDOMProcessingInstruction
Set pi = xmldoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""")
xmldoc.insertBefore pi, xmldoc.childNodes.Item(0)
Dim page As IXMLDOMElement
Set page = xmldoc.createElement("root")
Set xmldoc.documentElement = page
xmldoc.save fileName
writexml = False
Exit Function
End If
xmldoc.async = False
xmldoc.validateOnParse = False
xmldoc.Load (fileName)
Set iName = xmldoc.documentElement.selectSingleNode(itemName)
iName.Text = itemValue
xmldoc.save fileName
writexml = True
Set xmldoc = Nothing
Set objfso = Nothing
End Function
Set pi = xmldoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""")
xmldoc.insertBefore pi, xmldoc.childNodes.Item(0)
' top node
Dim process As IXMLDOMElement
Set process = xmldoc.createElement("process")
Set xmldoc.documentElement = process
'
Dim element As IXMLDOMElement
'
Set element = xmldoc.createElement("process-name")
element.Text = "aaa"
process.appendChild element xmldoc.Save "c:\test.xml"
不过 writexml 可以少作改进:Function writexml(fileName, itemName, itemValue) As Boolean
writexml = False
Dim xmlDoc As MSXML2.DOMDocument40
Dim objFso As FileSystemObject
Dim iName, rootNode As IXMLDOMElement
Set xmlDoc = New MSXML2.DOMDocument40
Set objFso = New Scripting.FileSystemObject
If Not objFso.fileexists(fileName) Then
Dim pi As IXMLDOMProcessingInstruction
Set pi = xmlDoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""")
xmlDoc.insertBefore pi, xmlDoc.childNodes.Item(0)
Dim page As IXMLDOMElement
Set page = xmlDoc.createElement("Configuration")
Set xmlDoc.documentElement = page
xmlDoc.save fileName
End If
xmlDoc.async = False
xmlDoc.validateOnParse = False
xmlDoc.Load (fileName)
Set iName = xmlDoc.documentElement.selectSingleNode(itemName)
If iName Is Nothing Then
Set iName = xmlDoc.createElement(itemName)
Set rootNode = xmlDoc.selectSingleNode("Configuration")
rootNode.appendChild iName
End If
iName.Text = itemValue
xmlDoc.save fileName
writexml = True
Set xmlDoc = Nothing
Set objFso = Nothing
End Function不知这样是否更加好用一点