【发布时间】:2015-12-29 16:18:47
【问题描述】:
我怎样才能改变我的xml的外观例如
<root>
<elem1>
<value>
122
</value>
<text>
This_is_just_a_text
</text>
</elem1>
<elem1>
<value>
122
</value>
<text>
This_is_just_a_text
</text>
</elem1>
</root>
看起来像:
<root>
<elem1>
<value>122</value>
<text>This_is_just_a_text</text>
</elem1>
<elem1>
<value>122</value>
<text>This_is_just_a_text</text>
</elem1>
</root>
我只是想知道发生这种情况的原因是什么?顺便说一下,下面的方法/函数用于添加缩进!
def prettify(elem):
"""
Return a pretty-printed XML string for the Element.
"""
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent="\t")
【问题讨论】:
-
当子元素是叶子时,你想将元素打印在一行中——这是你的美感还是什么原因?如果文本很长,你永远看不到结束标签怎么办。具有相同缩进级别的一个元素的开始和结束标记完全有意义。像男人一样对待它或给出任何改变外观的理由;)
标签: python xml python-2.7 python-3.x elementtree