Add prettifyNode tool function

This commit is contained in:
Robin Dunn
2015-02-26 16:20:31 -08:00
parent e308d980b2
commit 2e1b3ec664

View File

@@ -1541,6 +1541,7 @@ def flattenNode(node, rstrip=True):
Extract just the text from a node and its children, tossing out any child
node tags and attributes.
"""
# TODO: can we just use ElementTree.tostring for this function?
if node is None:
return ""
if sys.version_info < (3,):
@@ -1561,6 +1562,18 @@ def flattenNode(node, rstrip=True):
return text
def prettifyNode(elem):
"""
Return a pretty-printed XML string for the Element. Useful for debugging
or better understanding of xml trees.
"""
from xml.etree import ElementTree
from xml.dom import minidom
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def appendText(node, text):
"""
Append some text to a docstring element, such as an item's briefDoc or
@@ -1584,6 +1597,7 @@ def prependText(node, text):
node.insert(0, ele)
def makeTextElement(text):
element = et.Element('para')
element.text = text