【发布时间】:2016-11-29 23:48:49
【问题描述】:
我正在尝试找出一种方法来使用 ElementTree 附加到 XML 标记中的字符串。
基本上我想制作:
<gco:CharacterString>
2016-08-11 13:52:15 - Bob Smith
fourth comment yadayada
2016-08-11 13:53:34 - Bob Smith
third comment blah
2016-10-17 11:13:41 - Bob Smith
second comment
2016-10-25 10:53:19 - Bob Smith
first comment
</gco:CharacterString>
每次用户输入评论时,它都会附加评论并加盖日期。
我通常会这样构建一个标签:
historystrings = ET.SubElement(statement,"
{http://www.isotc211.org/2005/gco}CharacterString").text = (datestamp,comment) # SOURCE HERE
但不确定如何使用元素树进行追加,以便它保留以前条目的记录。
【问题讨论】:
-
旁白:您添加的数据是结构化的。在 XML 中维护该结构可能会更好,例如,
<comment><date>2016-08-11 13:52:15</date><author>Bob Smith</author><text>yada</text></comment> -
我已经考虑过了,但不幸的是,由于旧格式,我需要保持这种格式。
标签: python xml lxml elementtree