rendered paste body#!/usr/bin/python
import xml.dom.minidom, re
def remove_whilespace_nodes(node):
remove_list = []
for child in node.childNodes:
if child.nodeType == xml.dom.minidom.Node.TEXT_NODE:
child.data = child.data.strip()
if not child.data.strip():
remove_list.append(child)
elif child.hasChildNodes():
remove_whilespace_nodes(child)
for node in remove_list:
node.parentNode.removeChild(node)
return node
doc = remove_whilespace_nodes(xml.dom.minidom.parse("test.xml"))
fd = open("test.xml", "w")
fd.write(re.sub("\n[\s]*([^\n<]*)\n[\s]*</", "\\1</", doc.toprettyxml().replace('<?xml version="1.0" ?>\n', '')))
fd.close()