I have put some xml data in a file the python code below and have been trying to figure out how to get the data from the file add another "person" and then save to the same file again.
from lxml import etree
from lxml.builder import ElementMaker
E = ElementMaker()
DOC = E.doc
PERSON = E.activity
TITLE = E.title
DESC = E.desc
IDNO = E.idno
def NAME(*args):
return {"name":' '.join(args)}
thing = DOC (
PERSON( NAME("John"),
DESC ("germedabob"),
IDNO ("2")
)
)
filename = "xmltestthing.xml"
FILE = open(filename,"w")
FILE.writelines(etree.tostring(thing, pretty_print=True))
FILE.close()
This is what I have got so far, I can't work out how to add anything to it and save it again.
from lxml import etree
file = "xmltestthing.xml"
thing1 = etree.parse(file)
print(etree.tostring(thing1, pretty_print=True))
That outputs:
<doc>
<person name="John">
<desc>germedabob</desc>
<idno>2</idno>
</person>
</doc>
Thanks