首页 > 解决方案 > 在 Excel VBA 中使用 SAXXMLReader60 解析多个非常大的文件。如何停止解析文档并转到下一个文件?

问题描述

我试图在 Excel VBA 中相对快速地解析大型 XML 文件。在每个文件中,我只需要从前几个元素中获取信息,然后需要退出解析并转到下一个文件。我实现了 ContentHandlerError 但看不到如何提升它以便优雅地回到循环并解析下一个文件。这是代码,我真的很感激任何帮助!

Sub main()

    Dim saxReader As SAXXMLReader60
    Dim saxhandler As ContentHandlerImpl
    Dim saxerror As ContentHandlerError
    Dim sFolder As String
    Dim sFile As String
    Dim sFilePath As String

    sFilePath = "c:\testfiles\"
    sFolder = sFilePath & "*.xml"

    sFile = Dir(sFolder)

    Do While Len(sFile) > 0
        Debug.Print sFile

        Set saxReader = New SAXXMLReader60
        Set saxhandler = New ContentHandlerImpl
        Set saxerror = New ContentHandlerError

        Set saxReader.contentHandler = saxhandler
        Set saxReader.errorHandler = saxerror

        saxReader.parseURL "file://" & sFilePath & sFile

        Set saxReader = Nothing

        sFile = Dir
    Loop

End Sub

Private Sub IVBSAXContentHandler_endElement(strNamespaceURI As String, strLocalName As String, strQName As String)

    If strLocalName = "Series" Then

        'TODO:How to stop processing and go to next file?
    End If

End Sub

标签: excelvbasaxsaxparser

解决方案


推荐阅读