我正在对 XML 文件列表进行 XML 解析。我正在使用一个覆盖元素树的 XMLParser 类的模块。这是代码-
import sys
sys.modules['_elementtree'] = None
try:
sys.modules.pop('xml.etree.ElementTree')
except KeyError:
pass
import xml.etree.ElementTree as ET
class Parse():
def __init__(self):
self.xmlFiles = [list_of_xmlFile_paths]
def parse_xml_files(self):
for filepath in self.xmlFiles:
root = ET.parse(filepath, LineNumberingParser()).getroot()
for elem in root:
print(elem.start_line_numer, elem.end_line_number)
class LineNumberingParser(ET.XMLParser):
def _start(self, *args, **kwargs):
# Here we assume the default XML parser which is expat
# and copy its element position attributes into output Elements
self.element = super(self.__class__, self)._start(*args, **kwargs)
self.element.start_line_number = self.parser.CurrentLineNumber
self.element.start_column_number = self.parser.CurrentColumnNumber
return self.element
def _end(self, *args, **kwargs):
self.element = super(self.__class__, self)._end(*args, **kwargs)
self.element.end_line_number = self.parser.CurrentLineNumber
self.element.end_column_number = self.parser.CurrentColumnNumber
return self.element
LineNumberingParser 类为我提供了 xml 节点的开始行和结束行。我的问题是,对于每个 xml 文件,都会初始化类。所以这种重复初始化效率不高。我怎样才能通过只初始化一次类来做到这一点?任何人都可以请建议。