diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -549,10 +549,29 @@ """ def __init__(self, element=None, file=None): # assert element is None or iselement(element) + self._children = [] # comments and processing instructions only self._root = element # first node if file: self.parse(file) + def append(self, element): + """Add a Comment or ProcessingInstruction *element* to appear before + the ElementTree's root element. + + """ + tag = getattr(element, 'tag', None) + if not (tag is Comment or tag is PI): + raise TypeError('expected a Comment or ProcessingInstruction') + self._children.append(element) + + def remove(self, element): + """Remove matching element based on element identity. + + ValueError is raised if a matching element could not be found. + + """ + self._children.remove(subelement) + def getroot(self): """Return root element of this tree.""" return self._root @@ -765,6 +784,13 @@ declared_encoding = locale.getpreferredencoding() write("\n" % ( declared_encoding,)) + for elem in self._children: + tag = elem.tag + text = elem.text + if tag is Comment: + write("" % text) + elif tag is ProcessingInstruction: + write("" % text) if method == "text": _serialize_text(write, self._root) else: