Pretty printing XML in Python

后端 未结 26 1739
一个人的身影
一个人的身影 2020-11-22 02:18

What is the best way (or are the various ways) to pretty print XML in Python?

26条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 02:54

    from lxml import etree
    import xml.dom.minidom as mmd
    
    xml_root = etree.parse(xml_fiel_path, etree.XMLParser())
    
    def print_xml(xml_root):
        plain_xml = etree.tostring(xml_root).decode('utf-8')
        urgly_xml = ''.join(plain_xml .split())
        good_xml = mmd.parseString(urgly_xml)
        print(good_xml.toprettyxml(indent='    ',))
    

    It's working well for the xml with Chinese!

提交回复
热议问题