如何刪除Python 2.6中XML字符串中的空格和換行符?我嘗試了以下軟件包:刪除XML字符串中的空格
etree:這個片段可以保持原有的空格:
xmlStr = '''<root>
<head></head>
<content></content>
</root>'''
xmlElement = xml.etree.ElementTree.XML(xmlStr)
xmlStr = xml.etree.ElementTree.tostring(xmlElement, 'UTF-8')
print xmlStr
我不能使用Python 2.7,其將提供method
參數。
minidom命名:一樣的:
xmlDocument = xml.dom.minidom.parseString(xmlStr)
xmlStr = xmlDocument.toprettyxml(indent='', newl='', encoding='UTF-8')
這可能有助於使用LXML刪除從文本節點http://stackoverflow.com所有空行和空白頻段/ a/19396130/973699 – DevC 2013-10-16 06:09:42