【发布时间】:2011-03-19 15:28:59
【问题描述】:
如何在 Python 2.6 中删除 XML 字符串中的空格和换行符?我尝试了以下软件包:
etree:这个 sn-p 保留原始空格:
xmlStr = '''<root>
<head></head>
<content></content>
</root>'''
xmlElement = xml.etree.ElementTree.XML(xmlStr)
xmlStr = xml.etree.ElementTree.tostring(xmlElement, 'UTF-8')
print xmlStr
我不能使用提供 method 参数的 Python 2.7。
minidom:一样:
xmlDocument = xml.dom.minidom.parseString(xmlStr)
xmlStr = xmlDocument.toprettyxml(indent='', newl='', encoding='UTF-8')
【问题讨论】:
-
这可能有助于使用 lxml 从文本节点 stackoverflow.com/a/19396130/973699 中删除所有空白行和空格
标签: python xml xml-serialization python-2.6 elementtree