Learning Python Networking(Second Edition)
上QQ阅读APP看书,第一时间看更新

Pretty printing

It would be useful for us to be able to produce output in a more legible format, such as the example shown at the beginning of this section. To do this, we can use another XML API, minidom, provided by the standard library.

We can use the following commands to print some nicely formatted XML:

>>> import xml.dom.minidom as minidom
>>> print(minidom.parseString(ET.tostring(root)).toprettyxml())
<?xml version="1.0" ?>
<root>
<book>
<name>Book1</name>
</book>
</root>