def loop(self, e): if e.text: self.html.append(e.text) if e.getchildren(): for c in e.getchildren(): name = remove_ns(c.tag) if name in dir(self): getattr(self, name)(c) else: self.loop(c) lst = [] root = tree.getroot() xml = XMLTree() head = "<html><head><title>Title</title></head><body>" xml.html.append(head) """for e in root.getchildren(): xml.sourceDoc(e)""" xml.loop(root) end = "</body></html>" xml.html.append(end) with open("output.html", "w") as f: f.write("".join(xml.html))