ConvertCSVtoXML.vbs

本文介绍了一种使用VBScript将CSV文件转换为XML格式的方法。该脚本利用ADO和Microsoft XML DOM来读取CSV文件的内容并创建相应的XML文档。通过这种方式,可以方便地将表格数据重新格式化为XML,便于进一步的数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

'
On Error Resume Next
'convert CVS file to XML format
Dim objFSO
dim objXML
Dim objRoot
Dim objNode
Dim objAttrib
Dim objChildNode
Dim objConnection, objRecordset,objFields

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

strSource=OpenFilePath() 'You must return a full filename and path for the source CSV file.
strXMLFile=SaveAs("demo.xml") 'Specify the name of the XML file to create.

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

Set objFSO=CreateObject("Scripting.FileSystemObject")

'verify csv file exists just in case someone manually typed in a filename and path.
If objFSO.FileExists(strSource) Then
 tmpArray=Split(GetFilePath(strSource),",")
 strPathtoTextFile = tmpArray(0)
 strFile=tmpArray(1)
 'if path component not defined then exit script
 If strPathtoTextFile=" " Then
  WScript.Echo "Can't determine path to " & strSource & "."
  WScript.Quit
 End If
 
 objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & strPathtoTextFile & ";" & _
          "Extended Properties=""text;HDR=YES;FMT=Delimited"""

 objRecordset.Open "SELECT * FROM " & strFile, _
          objConnection, adOpenStatic, adLockOptimistic, adCmdText

 'Create XML document
 SET objXML = CreateObject("Microsoft.XMLDOM")
 set objRoot=objXML.createNode("element","Main","")
 objXML.appendChild(objRoot)
 set objAttrib=objXML.createAttribute("Created")
 objXML.documentElement.setAttribute "Created",Now
 set objAttrib=objXML.createAttribute("Source")
 objXML.documentElement.setAttribute "Source",strSource
 
 Set objFields=objRecordset.Fields
 Do Until objRecordset.EOF
 'Create an "Item" tag for each entry in the file
 Set objNode=objXML.createNode("Element","Item","")
  objRoot.appendChild(objNode)
  
 For z=0 To objFields.Count-1
  strHeading=objFields.Item(z).name
  'WScript.Echo "Adding element for " & strHeading
  Set objChildNode=objXML.createNode("Element",strHeading,"")
  'if value in CSV is blank
  If IsNull(objRecordset.Fields(strHeading)) Then
   strText="." 'we'll use a . so that the tag closes
  Else
   strText=objRecordset.Fields(strHeading)
  End If
  'set the value of the child node
  objChildNode.text=strText
  objNode.appendChild objChildNode
 Next
 objRecordset.MoveNext
 Loop
 objConnection.Close
 'commit XML changes to disk
 objXML.save(strXMLfile)

 'display a summary message
 WScript.Echo "Converted " & strSource & " to " & strXMLFile
Else
 'display error message and quit script
    WScript.Echo "Failed to find " & strSource
 WScript.quit
End If

WScript.Quit

'//////////////////////////////////////////////
'parse filename from path function
'//////////////////////////////////////////////
Function GetFilePath(strFile)
'returns an csv array of path component and file component
On Error Resume Next
strTmp=StrReverse(strFile)
strFileName=Left(strTmp,InStr(strTmp,"/")-1)
strFileName=StrReverse(strFileName)
strPath=Mid(strTmp,InStr(strTmp,"/"))
strPath=StrReverse(strPath)
GetFilePath=strPath & "," & strFileName

End Function

'//////////////////////////////////////////////
'Present OpenFile dialog box and return filename
'with path
'//////////////////////////////////////////////

Function OpenFilePath()

On Error Resume Next
Dim objDialog
Set objDialog=CreateObject("SAFRCFileDlg.FileOpen")

objDialog.OpenFileOpenDlg
srcFile=objDialog.FileName

OpenFilePath=srcFile
End Function

'//////////////////////////////////////////////
'Present the SaveAs dialogbox and return
'filename with path
'//////////////////////////////////////////////

Function SaveAs(strFile)
'requires Windows XP or later
Dim objDialog
Set objDialog=CreateObject("SAFRCFileDlg.FileSave")

objDialog.filename=strFile
objDialog.OpenFileSaveDlg
SaveAs=objDialog.FileName

End function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值