Remove whitespaces in XML string

前端 未结 8 1725
我在风中等你
我在风中等你 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:15

    xmlStr = xmlDocument.toprettyxml(indent='\t', newl='\n', encoding='UTF-8')
    fix = re.compile(r'((?<=>)(\n[\t]*)(?=[^<\t]))|(?<=[^>\t])(\n[\t]*)(?=<)')
    newXmlStr = re.sub(fix, '', xmlStr )
    

    from this source

提交回复
热议问题