prettyprint elementtree

import xml.etree.ElementTree as ET

root = ET.Element("html")

head = ET.SubElement(root, "head")

title = ET.SubElement(head, "title")
title.text = "Page Title"

body = ET.SubElement(root, "body")
body.set("bgcolor", "#ffffff")

body.text = "Hello, World!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)

from xml.dom.minidom import parseString as parseString

def prettyPrint(element):
txt = ET.tostring(element)
print parseString(txt).toprettyxml()

titleB = ET.SubElement(head, "titleB")
titleB.text = "Page Title"

prettyPrint(root)
you like ? Share it ,do it ..do it