Creating XML-child's from a list



I want to iterate over a list and create for each list-element a child entry in my XML-File. To create the XML-File I use lxml with etree. I could do:



heads = ["foo", "foobar", "fooboo"]

child1 = etree.SubElement(root, heads[0])
child2 = etree.SubElement(root, heads[1])
...


But I want to do it automatically - if there are 10 list items, there should be 10 child entries in the XML-File. I've tried something like this:



for i_c, i in enumerate(heads):
a = "child_%i = etree.SubElement(root, %s)" % (i_c, i)
exec a


Im pretty new to Python.. so please don't mind. :)


Greetings, Jan


No comments:

Post a Comment