5,139
社区成员
发帖
与我相关
我的任务
分享
Sub CreateXML()
Dim fileName As String
Dim objXML As Object
Set objXML = CreateObject("MSXml2.DOMDocument")
fileName = "E:\ForTest.xml"
If Not objXML.Load(fileName) Then
Err.Raise objXML.parseError.ErrorCode, , objXML.parseError.reason
End If
Dim point As Object
Set point = objXML.SelectSingleNode("root")
Dim attr As Object
Set attr = point.ChildNodes.Item(0)
'Debug.Print attr.Text
Dim strOutputPath As String
strOutputPath = "E:\outPut.xml"
Dim xmldoc, rootNode, Header, messageNode, typeNode As Object
Set xmldoc = CreateObject("MSXML2.DOMDocument")
xmldoc.PreserveWhitespace = True
Set rootNode = xmldoc.createElement("root")
Set xmldoc.DocumentElement = rootNode
Set Header = xmldoc.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'")
xmldoc.InsertBefore Header, xmldoc.ChildNodes(0)
Set messageNode = xmldoc.createElement("value")
rootNode.appendChild messageNode
messageNode.Text = attr.Text
xmldoc.Save strOutputPath
End Sub
Sub AddLineBreak(file As String)
Dim readFile As String, FSO, openFile As Object
If Dir(file) = "" Then MsgBox "Not found file!", vbExclamation: Exit Sub
Set FSO = CreateObject("Scripting.FileSystemObject")
Set openFile = FSO.OpenTextFile(file, 1, -1)
readFile = openFile.ReadAll
openFile.Close
Dim valuePre, rootSuf As String
valuePre = "<root>" & vbCrLf & vbTab & "<value>"
rootSuf = "</value>" & vbCrLf & "</root>"
Dim newFile As String
newFile = Replace(readFile, "<root><value>", valuePre)
newFile = Replace(newFile, "</value></root>", rootSuf)
Set openFile = FSO.OpenTextFile(file, 2, -1)
openFile.writeline newFile
openFile.Close
Set FSO = Nothing
Set openFile = Nothing
End Sub
另外请斑竹帮忙结贴吧,谢谢!