I am new to python / lxml. After reading the lxml site and plunging into python, I could not find a solution to my n00b problems. I have the following xml example:
--------------- <addressbook> <person> <name>Eric Idle</name> <phone type='fix'>999-999-999</phone> <phone type='mobile'>555-555-555</phone> <address> <street>12, spam road</street> <city>London</city> <zip>H4B 1X3</zip> </address> </person> </addressbook> -------------------------------
I am trying to add one child to the root element and write the whole file back as a new xml or more to write the existing xml. Currently, I am writing only one line.
from lxml import etree tree = etree.parse('addressbook.xml') root = tree.getroot() oSetroot = etree.Element(root.tag) NewSub = etree.SubElement ( oSetroot, 'CREATE_NEW_SUB' ) doc = etree.ElementTree (oSetroot) doc.write ( 'addressbook1.xml' )
TIA
python xml lxml
Nathaniel dickerson
source share