MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / TreeBuilder

Class TreeBuilder

tools/python-3.11.9-amd64/Lib/xml/etree/ElementTree.py:1396–1514  ·  view source on GitHub ↗

Generic element structure builder. This builder converts a sequence of start, data, and end method calls to a well-formed element structure. You can use this class to build an element structure using a custom XML parser, or a parser for some other XML-like format. *ele

Source from the content-addressed store, hash-verified

1394
1395
1396class TreeBuilder:
1397 """Generic element structure builder.
1398
1399 This builder converts a sequence of start, data, and end method
1400 calls to a well-formed element structure.
1401
1402 You can use this class to build an element structure using a custom XML
1403 parser, or a parser for some other XML-like format.
1404
1405 *element_factory* is an optional element factory which is called
1406 to create new Element instances, as necessary.
1407
1408 *comment_factory* is a factory to create comments to be used instead of
1409 the standard factory. If *insert_comments* is false (the default),
1410 comments will not be inserted into the tree.
1411
1412 *pi_factory* is a factory to create processing instructions to be used
1413 instead of the standard factory. If *insert_pis* is false (the default),
1414 processing instructions will not be inserted into the tree.
1415 """
1416 def __init__(self, element_factory=None, *,
1417 comment_factory=None, pi_factory=None,
1418 insert_comments=False, insert_pis=False):
1419 self._data = [] # data collector
1420 self._elem = [] # element stack
1421 self._last = None # last element
1422 self._root = None # root element
1423 self._tail = None # true if we're after an end tag
1424 if comment_factory is None:
1425 comment_factory = Comment
1426 self._comment_factory = comment_factory
1427 self.insert_comments = insert_comments
1428 if pi_factory is None:
1429 pi_factory = ProcessingInstruction
1430 self._pi_factory = pi_factory
1431 self.insert_pis = insert_pis
1432 if element_factory is None:
1433 element_factory = Element
1434 self._factory = element_factory
1435
1436 def close(self):
1437 """Flush builder buffers and return toplevel document Element."""
1438 assert len(self._elem) == 0, "missing end tags"
1439 assert self._root is not None, "missing toplevel element"
1440 return self._root
1441
1442 def _flush(self):
1443 if self._data:
1444 if self._last is not None:
1445 text = "".join(self._data)
1446 if self._tail:
1447 assert self._last.tail is None, "internal error (tail)"
1448 self._last.tail = text
1449 else:
1450 assert self._last.text is None, "internal error (text)"
1451 self._last.text = text
1452 self._data = []
1453

Callers 5

__init__Method · 0.85
XMLFunction · 0.85
XMLIDFunction · 0.85
fromstringlistFunction · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected