VBA可以使用MSXML2.Document来创建XML Dom树并输出到文件,先看个简单的例子:
Function CreateXml(xmlFile As String)
Dim xDoc As Object
Dim rootNode As Object
Dim header As Object
Dim newNode As Object
Dim tNode As Object
Set xDoc = CreateObject("MSXML2.DOMDocument")
Set rootNode = xDoc.createElement("BookList")
Set xDoc.DocumentElement = rootNode
'xDoc.Load xmlFile
Set header = xDoc.createProcessingInstruction("xml", "version='1.0' encoding='Unicode'")
xDoc.InsertBefore header, xDoc.ChildNodes(0)
Set newNode = xDoc.createElement("book")
Set tNode = xDoc.DocumentElement.appendChild(newNode)
tNode.setAttribute "type", "program"
Set newNode = xDoc.createElement("name")
Set tNode = xDoc.DocumentElement.ChildNodes.Item(0).appendChild(newNode)
tNode.appendChild (xDoc.createTextNode("Thinking in Java"))
Set newNode = xDoc.createElement("author")
Set tNode = xDoc.DocumentElement.ChildNodes.Item(0).appendChild(newNode)
tNode.appendChild (xDoc.createTextNode("Bruce Eckel"))
Set newNode = xDoc.createElement("book")
Set tNode = xDoc.DocumentElement.appendChild(newNode)
tNode.setAttribute "type", "literature"
Set newNode = xDoc.createElement("name")
Set tNode = xDoc.DocumentElement.ChildNodes.Item(1).appendChild(newNode)
tNode.appendCh