擅长:python、mysql、java
<p>取自<a href="http://ruslanspivak.com/2014/05/12/how-to-pretty-print-xml-with-lxml/" rel="nofollow">http://ruslanspivak.com/2014/05/12/how-to-pretty-print-xml-with-lxml/</a></p>
<pre><code>import StringIO
import lxml.etree as etree
def prettify(xml_text):
"""Pretty prints xml."""
parser = etree.XMLParser(remove_blank_text=True)
file_obj = StringIO.StringIO(xml_text)
tree = etree.parse(file_obj, parser)
return etree.tostring(tree, pretty_print=True)
</code></pre>