【问题标题】:Append to text inside XML tag附加到 XML 标记内的文本
【发布时间】: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 中维护该结构可能会更好,例如,&lt;comment&gt;&lt;date&gt;2016-08-11 13:52:15&lt;/date&gt;&lt;author&gt;Bob Smith&lt;/author&gt;&lt;text&gt;yada&lt;/text&gt;&lt;/comment&gt;
  • 我已经考虑过了,但不幸的是,由于旧格式,我需要保持这种格式。

标签: python xml lxml elementtree


【解决方案1】:

首先,您需要以某种方式访问​​现有元素。比如像这样:

gco_cs = root.find('{http://www.isotc211.org/2005/gco}CharacterString')

然后你可以修改.text属性,像这样:

gco_cs.text += '\nSome new data\n'

这是一个完整的例子:

foo.py

import xml.etree.ElementTree as ET
tree = ET.parse('foo.xml')
root = tree.getroot()

gco_cs = root.find('{http://www.isotc211.org/2005/gco}CharacterString')
gco_cs.text += '\nSome new data\n'

ET.dump(root)

foo.xml

<foo xmlns:gco="http://www.isotc211.org/2005/gco">
<gco:CharacterString>
some text
</gco:CharacterString>
</foo> 

结果:

$ python foo.py 
<foo xmlns:ns0="http://www.isotc211.org/2005/gco">
<ns0:CharacterString>
some text

Some new data
</ns0:CharacterString>
</foo>

【讨论】:

    【解决方案2】:

    看看我为自己编写的这部分脚本:

    class Module():
    
    def moduleLine(self):
    
        with open("/root/custom-nginx/nginx/debian/rules","r") as rules:
    
            lines = rules.readlines()
    
            for line in lines:
    
                if line.startswith("full_configure_flags"):
                    full_index = str(line)
    
            findex = lines.index(full_index)
    
        for line in lines[int(findex)+1:]:
    
            if line.endswith(":= \\\n"):
                second_index = str(line)
                break
            else:
                continue
    
        sindex = lines.index(second_index)
        add_line = sindex-3
    
        rules.close()
        return add_line
    
    def addModule(self,index):
    
        with open("/root/custom-nginx/nginx/debian/rules", "r") as file:
            data = file.readlines()
            data[index] = data[index] + "\t"*3 + "--add-module=$(MODULESDIR)/ngx_pagespeed \\" + "\n"
        file.close()
        with open("/root/custom-nginx/nginx/debian/rules","w") as file:
            file.writelines(data)
        file.close()
    

    在 'moduleLine' 函数中,它会打开一个名为 rules 的文件,并通过 readlines()(行变量)读取该行

    之后 if 语句出现并检查该行是否与我想要的字符串匹配,并且 findex 包含行号。

    如果你有一个特定的 xml 文件并且你知道行号,你可以直接在你的 python 代码中使用行号。

    查看addModule 函数,它在data[index] 处附加 --add-module=$(MODULESDIR)/ngx_pagespeed \" + "\n" 字符串,索引为行号。

    您可以使用此基础将您的字符串附加到您的 xml 文件中。

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 2019-05-08
      • 2013-05-13
      • 1970-01-01
      相关资源
      最近更新 更多