用法:
class xml.etree.ElementTree.XMLParser(*, target=None, encoding=None)
此類是模塊的低級構建塊。它使用
xml.parsers.expat
進行高效的、基於事件的 XML 解析。它可以使用feed()
方法以增量方式提供XML 數據,並且解析事件被轉換為推送API - 通過在target
對象上調用回調。如果省略target
,則使用標準TreeBuilder
。如果給定encoding
1,則該值將覆蓋 XML 文件中指定的編碼。在 3.8 版中更改:參數現在僅關鍵字.這
html
論點不再支持。XMLParser.feed()
為每個開始標簽調用target
的start(tag, attrs_dict)
方法,為每個結束標簽調用其end(tag)
方法,數據由方法data(data)
處理。有關進一步支持的回調方法,請參閱TreeBuilder
類。XMLParser.close()
調用target
的方法close()
。XMLParser
不僅可以用於構建樹結構。這是一個計算 XML 文件最大深度的示例:>>> from xml.etree.ElementTree import XMLParser >>> class MaxDepth: # The target object of the parser ... maxDepth = 0 ... depth = 0 ... def start(self, tag, attrib): # Called for each opening tag. ... self.depth += 1 ... if self.depth > self.maxDepth: ... self.maxDepth = self.depth ... def end(self, tag): # Called for each closing tag. ... self.depth -= 1 ... def data(self, data): ... pass # We do not need to do anything with data. ... def close(self): # Called when all data has been parsed. ... return self.maxDepth ... >>> target = MaxDepth() >>> parser = XMLParser(target=target) >>> exampleXml = """ ... <a> ... <b> ... </b> ... <b> ... <c> ... <d> ... </d> ... </c> ... </b> ... </a>""" >>> parser.feed(exampleXml) >>> parser.close() 4
相關用法
- Python xml.etree.ElementTree.Element用法及代碼示例
- Python xml.etree.ElementTree.canonicalize用法及代碼示例
- Python xml.parsers.expat.ExpatError.code用法及代碼示例
- Python xml.parsers.expat.ParserCreate用法及代碼示例
- Python xml.dom.pulldom.DOMEventStream.expandNode用法及代碼示例
- Python xdrlib.Packer.pack_list用法及代碼示例
- Python cudf.core.column.string.StringMethods.is_vowel用法及代碼示例
- Python torch.distributed.rpc.rpc_async用法及代碼示例
- Python torch.nn.InstanceNorm3d用法及代碼示例
- Python sklearn.cluster.MiniBatchKMeans用法及代碼示例
- Python pandas.arrays.IntervalArray.is_empty用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python numpy.less()用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python Sympy Permutation.list()用法及代碼示例
- Python dask.dataframe.Series.apply用法及代碼示例
- Python networkx.algorithms.shortest_paths.weighted.all_pairs_dijkstra_path用法及代碼示例
- Python scipy.ndimage.binary_opening用法及代碼示例
- Python pyspark.pandas.Series.dropna用法及代碼示例
- Python torchaudio.transforms.Fade用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 xml.etree.ElementTree.XMLParser。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。