如何解决如何使用Elementtree python删除结束标签?
我有一个将文本转换为所需 XML 的函数。我想删除子元素的结束标记并将文件另存为 XML。我该怎么做?
我使用的脚本:
from xml.etree.ElementTree import Element,SubElement,tostring,ElementTree
top = Element('top')
child = SubElement(top,'child')
child.text = 'This child contains text.'
child_with_tail = SubElement(top,'child_with_tail')
child_with_tail.text = 'This child has regular text.'
child_with_tail.tail = 'And "tail" text.'
child_with_entity_ref = SubElement(top,'child_with_entity_ref')
child_with_entity_ref.text = 'This and that'
tree = ElementTree(top)
tree.write('tree.xml',encoding ='utf-8',xml_declaration = True)
print(tostring(top))
输出:
<top><child>This child contains text.</child>
<child_with_tail>This child has regular text.</child_with_tail>And "tail" text.
<child_with_entity_ref>This and that</child_with_entity_ref></top>
我想在将其保存为 XML 文件之前删除 。所需的输出应如下所示:
<top><child>This child contains text.</child>
<child_with_tail>This child has regular text.</child_with_tail>And "tail" text.
<child_with_entity_ref>This and that</top>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。