All pastes #13442 Raw Edit

Unnamed

public text v1 · immutable
#13442 ·published 2005-06-05 18:43 UTC
rendered paste body
#!/usr/bin/python
import xml.dom.minidom

def do(lines):
    indent = -1
    previous_was_text = False
    for line in lines:
        line = line.strip()
        if line == "":
            pass
        elif line[0:2] == "</":
            indent -= 1
            if previous_was_text == True:
                previous_was_text = False
                yield line
            else:
                yield "\n" + indent*"\t" + line
        elif line[0] == "<":
            yield "\n" + indent*"\t" + line
            indent += 1
        else:
            yield line
            previous_was_text = True

doc = xml.dom.minidom.parse("test.xml")
fd = open("test.xml", "w")
fd.write("".join(do(doc.toprettyxml().replace('<?xml version="1.0" ?>\n', '').split("\n"))))
fd.close()