How can I print out the xml beautifully?
There is an xml file like this
<foo>
<bar>
<type foobar="1"/>
<type foobar="2"/>
</bar>
</foo>
When running Python code
import xml.dom.minidom
xml = xml.dom.minidom.parse("myxml.xml")
pretty_xml_as_string = xml.toprettyxml()
print(pretty_xml_as_string)
Result:
<?xml version="1.0" ?>
<foo>
<bar>
<type foobar="1"/>
<type foobar="2"/>
</bar>
</foo>
There are too many indentations.
xml python pretty-print
lxml.etree.parse outputs the xml file itself without opening.
import lxml.etree as etree
x = etree.parse("myxml.xml")
print etree.tostring(x, pretty_print = True)
Result:
<foo>
<bar>
<type foobar="1"/>
<type foobar="2"/>
</bar>
</foo>
© 2024 OneMinuteCode. All rights reserved.