Pretty printing XML in Python

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

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

相关标签:
26条回答
  • 2020-11-22 03:11
    import xml.dom.minidom
    
    dom = xml.dom.minidom.parse(xml_fname) # or xml.dom.minidom.parseString(xml_string)
    pretty_xml_as_string = dom.toprettyxml()
    
    0 讨论(0)
  • 2020-11-22 03:11

    As others pointed out, lxml has a pretty printer built in.

    Be aware though that by default it changes CDATA sections to normal text, which can have nasty results.

    Here's a Python function that preserves the input file and only changes the indentation (notice the strip_cdata=False). Furthermore it makes sure the output uses UTF-8 as encoding instead of the default ASCII (notice the encoding='utf-8'):

    from lxml import etree
    
    def prettyPrintXml(xmlFilePathToPrettyPrint):
        assert xmlFilePathToPrettyPrint is not None
        parser = etree.XMLParser(resolve_entities=False, strip_cdata=False)
        document = etree.parse(xmlFilePathToPrettyPrint, parser)
        document.write(xmlFilePathToPrettyPrint, pretty_print=True, encoding='utf-8')
    

    Example usage:

    prettyPrintXml('some_folder/some_file.xml')
    
    0 讨论(0)
提交回复
热议问题