An XML element hierarchy. This class also provides support for serialization to and from standard XML. *element* is an optional root element node, *file* is an optional file handle or file name of an XML file whose contents will be used to initialize the tree with.
| 524 | |
| 525 | |
| 526 | class ElementTree: |
| 527 | """An XML element hierarchy. |
| 528 | |
| 529 | This class also provides support for serialization to and from |
| 530 | standard XML. |
| 531 | |
| 532 | *element* is an optional root element node, |
| 533 | *file* is an optional file handle or file name of an XML file whose |
| 534 | contents will be used to initialize the tree with. |
| 535 | |
| 536 | """ |
| 537 | def __init__(self, element=None, file=None): |
| 538 | # assert element is None or iselement(element) |
| 539 | self._root = element # first node |
| 540 | if file: |
| 541 | self.parse(file) |
| 542 | |
| 543 | def getroot(self): |
| 544 | """Return root element of this tree.""" |
| 545 | return self._root |
| 546 | |
| 547 | def _setroot(self, element): |
| 548 | """Replace root element of this tree. |
| 549 | |
| 550 | This will discard the current contents of the tree and replace it |
| 551 | with the given element. Use with care! |
| 552 | |
| 553 | """ |
| 554 | # assert iselement(element) |
| 555 | self._root = element |
| 556 | |
| 557 | def parse(self, source, parser=None): |
| 558 | """Load external XML document into element tree. |
| 559 | |
| 560 | *source* is a file name or file object, *parser* is an optional parser |
| 561 | instance that defaults to XMLParser. |
| 562 | |
| 563 | ParseError is raised if the parser fails to parse the document. |
| 564 | |
| 565 | Returns the root element of the given source document. |
| 566 | |
| 567 | """ |
| 568 | close_source = False |
| 569 | if not hasattr(source, "read"): |
| 570 | source = open(source, "rb") |
| 571 | close_source = True |
| 572 | try: |
| 573 | if parser is None: |
| 574 | # If no parser was specified, create a default XMLParser |
| 575 | parser = XMLParser() |
| 576 | if hasattr(parser, '_parse_whole'): |
| 577 | # The default XMLParser, when it comes from an accelerator, |
| 578 | # can define an internal _parse_whole API for efficiency. |
| 579 | # It can be used to parse the whole source without feeding |
| 580 | # it with chunks. |
| 581 | self._root = parser._parse_whole(source) |
| 582 | return self._root |
| 583 | while True: |
no outgoing calls
no test coverage detected