2014-12-07 75 views
0

我有> 100MB(超過20L行)的XML文件,我無法直接將它傳遞給我的一個進程。我需要通過假設一些分隔符將其拆分爲多個較小的文件,並嘗試在VBScript中使用FileSystemObject以及BATCH文件選項。兩者都需要超過8分鐘才能讀取並創建10000行的第一個小文件。將大文件讀取和分割爲多個小文件的最佳方法

請爲此任務傳達一些良好的性能導向選項。

感謝您的幫助。

方法一:

Function SplitXML() 
Dim oSrcFile, oTgtFile, strHeader, intFiles, strContent, intSize 
Dim arrLines() 
Set oFSO = CreateObject("Scripting.FileSystemObject") 

strFilePath = Application.GetOpenFilename 
intLinesToSplit = InputBox("Enter the No of Lines to split with for each file:", "XML Splitter", 10000) 
strTgtPath = Replace(strFilePath, oFSO.GetFileName(strFilePath), "") 
strFileName = Replace(oFSO.GetFileName(strFilePath), ".xml", "") 
Set oSrcFile = oFSO.OpenTextFile(strFilePath) 


'Headers 
strHeader = "": strContent = "" 
Do 
    strTemp = oSrcFile.ReadLine 
    strHeader = strHeader & vbNewLine & strTemp 
Loop While InStr(1, strTemp, "</BLHeader>", 1) <= 0 

'Split 
intTemp = 0: intFiles = 0: blnNewFile = True: intSize = -1 
Do While Not oSrcFile.AtEndOfStream 
    intTemp = intTemp + 1 

    'Content 
    'intSize = intSize + 1 
    'ReDim Preserve arrLines(intSize) 
    'arrLines(intSize) = oSrcFile.ReadLine 
    strTemp = oSrcFile.ReadLine 
    strContent = strContent & vbNewLine & strTemp 

    If intTemp >= intLinesToSplit Then 
     If InStr(1, strTemp, "</EndingTag>", 1) > 0 Then 
      'Add Header 
      intFiles = intFiles + 1 
      Set oTgtFile = oFSO.CreateTextFile(strTgtPath & strFileName & "_" & intFiles & ".xml", True) 
      oTgtFile.WriteLine strHeader 

      'Add Content 
      oTgtFile.WriteLine strContent 'Join(arrLines, vbNewLine) 

      'Add tail 
      oTgtFile.WriteLine "</FinalFileTag>" 
      oTgtFile.Close 
     End If 
    End If 
Loop 

oSrcFile.Close 

端功能

方法2:

@echo off 
setlocal EnableDelayedExpansion 

set InFile=c:\ee\EE28352646\in.txt 
set OutDir=c:\ee\EE28352646 
REM Can not be larger than 2147483648 !!! 
set MaxLines=1000000 

if not exist "%InFile%" (
    echo *ERROR* Input file does not exist! 
    exit /b 
) 

if not exist "%OutDir%\" (
    echo *ERROR* Output folder does not exist! 
    exit /b 
) 

for %%A in ("%InFile%") do (
    set Name=%%~nA 
    set Ext=%%~xA 
) 

set /a Line=MaxLines+1 
set File=0 
for /f "usebackq tokens=*" %%A in ("%InFile%") do (
    set /a Line+=1 
    if !Line! GTR %MaxLines% (
    set /a File+=1 
    set OutFile=%OutDir%\%Name%_!File!%Ext% 
    if exist "!OutFile!" del "!OutFile!" 
    set Line=1 
) 
    echo.%%A>>"!OutFile!" 
) 
+0

沒有什麼可以在你的頭文件中優化的部分,恕我直言。但是在你的'Split'部分:首先,事先做好'oTgtFile.WriteLine strHeader',然後你可以一行一行地寫入內容:'oTgtFile.WriteLine strTemp'。避免內存和時間耗盡'strContent = strContent&vbNewLine&strTemp' – JosefZ 2014-12-08 00:06:21

回答

2

分裂任何XML文件的最好方法是-as始終在線使用一個實際的XML解析器的任務。假設你有一個這樣的XML文件:

<?xml version="1.0"?> 
<Foo> 
    <Bar>some</Bar> 
    <Bar>other</Bar> 
</Foo> 

,並想將它拆分成單獨的文件

<?xml version="1.0"?> 
<Foo> 
    <Bar>some</Bar> 
</Foo> 

<?xml version="1.0"?> 
<Foo> 
    <Bar>other</Bar> 
</Foo> 

你可以做這樣的事情:

numFiles = 4 'number of output files 

Set src = CreateObject("Msxml2.DOMDocument.6.0") 
src.async = False 
src.load "C:\path\to\input.xml" 

Set nodes = src.selectNodes("//Bar") 
numNodes = nodes.length \ numFiles 'number of nodes per output file 

Set xml = Nothing 
For i = 0 To nodes.length - 1 
    'create a new XML object on the first iteration and every time numNodes 
    'nodes have been added to the current object 
    If i Mod numNodes = 0 Then 
    If Not xml Is Nothing Then 
     'if we already have an XML object: save it to a file 
     Set prolog = xml.createProcessingInstruction("xml", "version='1.0'") 
     xml.insertBefore prolog, xml.childNodes(0) 
     xml.save "C:\path\to\output" & (i \ numNodes - 1) & ".xml" 
    End If 
    Set xml = CreateObject("Msxml2.DOMDocument.6.0") 
    Set root = xml.createElement("Foo") 
    xml.appendChild root 
    End If 
    root.appendChild nodes.item(i) 
Next 
'save unsaved XML object 
If Not xml Is Nothing Then 
    Set prolog = xml.createProcessingInstruction("xml", "version='1.0'") 
    xml.insertBefore prolog, xml.childNodes(0) 
    xml.save "C:\path\to\output" & (i \ numNodes - 1) & ".xml" 
End If 
相關問題