Getting a list of XML tags in file, using xml.etree.ElementTree Getting a list of XML tags in file, using xml.etree.ElementTree xml xml

Getting a list of XML tags in file, using xml.etree.ElementTree


I've done more of a research on the subject and found out suitable solution. Since this could be a common task to do, I'll answer it, hence I believe it could help others.

What I was looking for was etree method iter.

import xml.etree.ElementTree as ET# load and parse the filexmlTree = ET.parse('myXMLFile.xml')elemList = []for elem in xmlTree.iter():    elemList.append(elem.tag)# now I remove duplicities - by convertion to set and back to listelemList = list(set(elemList))# Just printing out the resultprint(elemList)

Important notes

  • xml.etree.ElemTree is a standard Python library
  • sample is written for Python v3.2.3
  • mechanic used to remove duplicities is based on converting to set, which allows only unique values and then converting back to list.


You could use the built-in Python set comprehension:

import xml.etree.ElementTree as ETxmlTree = ET.parse('myXMLFile.xml')tags = {elem.tag for elem in xmlTree.iter()}

If you specifically need a list, you can cast it to a list:

import xml.etree.ElementTree as ETxmlTree = ET.parse('myXMLFile.xml')tags = list({elem.tag for elem in xmlTree.iter()})