Steve Long
Steve Long

Reputation: 111

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

Upvotes: 3

Views: 1873

Answers (1)

Steve Long
Steve Long

Reputation: 111

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. :-)

Upvotes: 6

Related Questions