Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating xml doc from scratch with ElementTree

I'm getting an error from the following code:

import xml.etree.ElementTree as ET

tree = ET.ElementTree()
root = ET.Element("configuration")
elem = ET.SubElement(root, "appConfig")
elem2 = ET.SubElement(root, "fileGDB")
print ET.tostring(root)
tree.write(sys.stdout)

It throws an exception on the following line in the ElementTree.py module: iterate = elem.getiterator # cET compatibility The exception is: AttributeError: 'NoneType' object has no attribute 'getiterator'

In the code above:

print ET.tostring(root)

prints out just fine. I don't understand why it's throwing this exception. What am I doing wrong?

Steve

like image 422
Steve Long Avatar asked Jun 04 '26 22:06

Steve Long


1 Answers

Okay duh, I needed to make the following line the last after creating nodes:

tree = ET.ElementTree(root)

Maybe someone else will be as numbskull as I was. :-)

like image 51
Steve Long Avatar answered Jun 06 '26 11:06

Steve Long