Remove whitespaces in XML string

前端 未结 8 1737
我在风中等你
我在风中等你 2020-11-29 04:53

How can I remove the whitespaces and line breaks in an XML string in Python 2.6? I tried the following packages:

etree: This snippet keeps the original whitespaces:<

8条回答
  •  暖寄归人
    2020-11-29 05:29

    The only thing that bothers me about xml.dom.minidom's toprettyxml() is that it adds blank lines. I don't seem to get the split components, so I just wrote a simple function to remove the blank lines:

    #!/usr/bin/env python
    
    import xml.dom.minidom
    
    # toprettyxml() without the blank lines
    def prettyPrint(x):
        for line in x.toprettyxml().split('\n'):
            if not line.strip() == '':
                print line
    
    xml_string = "\nsomething\nparrot\n"
    
    # parse XML
    x = xml.dom.minidom.parseString(xml_string)
    
    # clean
    prettyPrint(x)
    

    And this is what the code outputs:

    
    
            something
            parrot
    
    

    If I use toprettyxml() by itself, i.e. print(toprettyxml(x)), it adds unnecessary blank lines:

    
    
    
    
            something
    
    
            parrot
    
    
    
    

提交回复
热议问题