Pretty printing XML in Python

后端 未结 26 1807
一个人的身影
一个人的身影 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

    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')
    

提交回复
热议问题