在 sax 中提前结束解析 xml

End parsing xml early in sax

当我使用xml.sax解析xml文件时,我想随时停止解析器或直到程序满足特定条件。请不要退出或抛出异常,因为我要程序继续运行.

如 'endElement' 中所述,如何提前完成解析并打印出来,例如'hello' 在 sax_parse?

class myHandler(sax.ContentHandler):
    def __init__(self):
        super().__init__()
        self.count = 0
        
    def startElement(self, name, attrs):
        if name == 'row':
            self.count += 1

    def endElement(self, name):
        if self.count > 10000:
            # stop the parser
            pass

def sax_parse():
    parser = sax.make_parser()
    parser.setFeature(sax.handler.feature_namespaces, 0)
    handler = myHandler()
    parser.setContentHandler(handler)
    parser.parse("url")
    print('hello')

下面的代码应该适合你。

这个想法是引发一个特定的异常,捕获它并忽略它..

class HaveEnoughRows(Exception):
    pass

class myHandler(sax.ContentHandler):
    def __init__(self):
        super().__init__()
        self.count = 0
        
    def startElement(self, name, attrs):
        if name == 'row':
            self.count += 1

    def endElement(self, name):
        if self.count > 10000:
            # stop the parser
            raise HaveEnoughRows()

def sax_parse():
    parser = sax.make_parser()
    parser.setFeature(sax.handler.feature_namespaces, 0)
    handler = myHandler()
    parser.setContentHandler(handler)
    try:
        parser.parse("url")
    except HaveEnoughRows:
        pass
    print('hello')