views:

28

answers:

1

Just a beginner question. I'm using "xml.etree.ElementTree" module to create an XML document with Python 3.1 from another structured document.

However, I cannot seem to locate an ElementTree function that returns the index of an existing subelement. I'm sure it's in there somewhere.

Any help would be much appreciated.

Thanks,

+1  A: 

The getchildren method returns a list of sub-elements of an Element object. You could then use the built-in index method of a list.

>>> import xml.etree.ElementTree as ET
>>> root = ET.Element("html")
>>> head = ET.SubElement(root, "head")
>>> body = ET.SubElement(root, "body")
>>> root.getchildren().index(body)
1
Mark
Thanks, exactly what I was looking for.
John