Infinity8
Infinity8

Reputation: 705

Append to text inside XML tag

I am trying to figure out a way to append to a string within an XML Tag using ElementTree.

Basically I want to produce:

<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>

And each time a user goes in to enter a comment it will append it and date stamp it.

I normally build a tag like this:

historystrings = ET.SubElement(statement,"
{http://www.isotc211.org/2005/gco}CharacterString").text = (datestamp,comment)  # SOURCE HERE

But not sure how to append using Element Tree so that it keeps a record of previous entries.

Upvotes: 1

Views: 2813

Answers (2)

Sadegh Alirezaie
Sadegh Alirezaie

Reputation: 383

Have a look at this part of script that I've wrote for myself:

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()

at the 'moduleLine' function it opens a file named rules and reads the line by readlines() (lines variable)

after that the if statement comes and checks the line if it matches the string that I want and findex contains the line number.

if you have an specific xml file and you know the line number you can use line number directly in your python code.

look at the addModule function, it appends the string of --add-module=$(MODULESDIR)/ngx_pagespeed \" + "\n" at the data[index] which index is the line number.

you can use this basis to append your string to your xml file.

Upvotes: 0

Robᵩ
Robᵩ

Reputation: 168626

First, you need to gain access to the existing element somehow. For example, like so:

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

Then you can modify the .text attribute, like so:

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

Here is a complete example:

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> 

Result:

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

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

Upvotes: 4

Related Questions