如何加快从磁盘加载XML文档的速度?

问题描述 投票:0回答:2

我正在尝试加快VB6 XML解析器的速度。要解析的XML文件存储在我的本地硬盘上。根据我的分析结果,下面的If xDOC.Load(objFile.Path) Then语句花费很长时间。在处理一小批100个XML文件期间,该语句总共花费了34.5秒。 XML文件样本为here。是否可以改进此代码以加快XML文件的加载速度,或者加载速度受XML文件本身的性质限制?

Option Explicit

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object

Dim xDOC As MSXML2.DOMDocument

Dim xPE As MSXML2.IXMLDOMParseError

Sub Main()

Set xDOC = New DOMDocument

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder("C:\My XML File Folder")

For Each objFile In objFolder.Files
    Set xDOC = New DOMDocument
    xDOC.async = False

    If xDOC.Load(objFile.Path) Then
        ' process the file
    Else
        ' XML file failed to load; log error and continue with next file
    End If

   Set xDOC = Nothing

Next objFile

Set objFolder = Nothing

End Sub
xml performance dom xml-parsing vb6
2个回答
1
投票

您的XML很好,实际上它很小,并且使用正确的文档设置可以非常快速地加载。

我确实注意到了DTD,每次下载文件时都会从http://patents.ic.gc.ca/cipo/dtd/ca-patent-document-v2-0.dtd重新下载DTD。此外,DTD本身会嵌入其他DTD文件,因此您也可能会下载它们。

MSXML XML默认情况下会做很多事情,但是,如果您的XML已知是“好”的,那么最快的加载方法是在调用Load()之前将以下值设置为false。这样,您只需要验证XML的格式是否正确。

var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.validateOnParse = false;  // don't validate
doc.resolveExternals = false; // don't even download external files (DTDs...)
doc.preserveWhiteSpace = false; // don't try to preserve formatting.
doc.load("somexml.xml");

希望这对您有所帮助,您可以通过VB6进行翻译


0
投票

在Windows中使用以下命令运行此命令

cscript.exe testperf.js testfile.xml 1000

这是testperf.js

var aArguments = WScript.Arguments;
var xmlDoc;
var xslDoc;

function loadXMLFile( strFileName ) {
    var xml = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
    xml.setProperty("SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'");
    xml.validateOnParse = false;
    xml.resolveExternals = false;
    xml.preserveWhiteSpace = false;
    if( !xml.load( strFileName ) ) {
        var strError = "";
        var facility = xml.parseError.errorCode>>16 & 0x1FFF;
        var code = xml.parseError.errorCode & 0xFFFF;
        strError = 'Error loading: ' + strFileName + '\r\n';
        strError += xml.parseError.reason;
        strError += "Facility: " + facility + " Code: " + code + "\r\n";
        strError += xml.parseError.srcText + "\r\n";
        strError += xml.parseError.url + "\r\n";
        strError += "Line: " + xml.parseError.line +  " Postion: " + xml.parseError.linepos + "\r\n";
        throw new Error( xml.parseError.errorCode, strError );
    }
    return xml;
}

try {
    if( aArguments.length < 2 ) {
        WScript.Echo( "Usage: testperf file.xml loadcount" );
        WScript.Quit( 1 );
    }
    var strStatus = 'Loading XML';

    var dtStart = new Date().valueOf();
    var nLoop = parseInt( aArguments(1) );
    for( i = 0; i < nLoop; i++ ) {
        xmlDoc = loadXMLFile( aArguments(0) );
    }
    var dtStop = new Date().valueOf();
    WScript.Echo( nLoop + " XML loads took " + parseFloat( (dtStop - dtStart) / 1000 ).toFixed( 2 ) + " seconds.");

}
catch( e ) {
    WScript.Echo( 'Error in file:' + aArguments(1) + '\n' + e.number + " " + e.description );
    WScript.Quit( 1 );
}
© www.soinside.com 2019 - 2024. All rights reserved.