Yes, in the package xml.etree you can find the built-in function related to XML. (also available for python2)

The one specifically you are looking for is findall.

For example:

import xml.etree.ElementTree as ET
tree = ET.fromstring(some_xml_data)
all_name_elements = tree.findall('.//name')

With:

In [1]: some_xml_data = "<help><person><name>dean</name></person></help>"

I get the following:

In [10]: tree.findall(".//name")
Out[10]: [<Element 'name' at 0x7ff921edd390>]
Answer from Dean Fenster on Stack Overflow
🌐
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
Finds all matching subelements, by tag name or path. Returns a list containing all matching elements in document order. namespaces is an optional mapping from namespace prefix to full name.
Discussions

Navigating and extracting XML in Python
Hi, I’m trying to find a way to extract certain elements from the note data in this xml document: F 1 4 3 1 half I now need the data from the tied element, and whilst extracting the data from pitch, I had no problem, as I simply itterated through pitch as follows: for pitch in myroot.ite... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
15
0
February 8, 2023
finding element by tag in xml
Hi I am trying to search an element by tag and new in reading a xml file (in python). I coded this , but it did not work ---------------------------------------------- '''This is to detect the first element and print out all that element by tag''' from xml.dom.minidom import parse from... More on thecodingforums.com
🌐 thecodingforums.com
2
February 20, 2010
python - Parsing XML to get all elem.tag: elem.text pairs - Code Review Stack Exchange
I am parsing ICD-10 codes and have achieved a result that satisfies my base case, but I'm worried about how fragile my method is: import xml.etree.ElementTree as ET # A sample of the larger XML f... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
June 2, 2017
Extract information from an xml file
I have an xml file looks like this #root element #child elements #the sub-child elements I want to get name 25 ASN #Lots of elements after this 26 ALA #... More on discuss.python.org
🌐 discuss.python.org
1
0
June 12, 2023
🌐
Reddit
reddit.com › r/learnpython › using elementtree to find nodes/tags in an xml document, without naming the tag parent nor the child.
r/learnpython on Reddit: Using ElementTree to find nodes/tags in an xml document, without naming the tag parent NOR the child.
June 21, 2021 -

Hello,

I was able to locate a child tag using its name + his parent attributes like this :

d=soup.findall(".//*[@attr='value']/childtag[@attr2='value2'])

it works fine, but whenever i remove the child tag like this :

d=soup.findall(".//*[@attr='value']/[@attr2='value2'])

i get no result.
Any idea how to manage to find the child tag without naming it? (only using the information of its attrib?)

Thanks

🌐
AskPython
askpython.com › home › python xml parser
Python XML Parser - AskPython
July 7, 2022 - ... <data> <items> <item ... the attribute value of name, we can do the same as before. We can also use tag.attrib[name] to get the value....
🌐
DataCamp
datacamp.com › tutorial › python-xml-elementtree
Python XML Tutorial: Element Tree Parse & Read | DataCamp
December 10, 2024 - Parse and read XML data with Element Tree Python package. Learn how to use xml.etree.elementtree and explore your data through XML today!
🌐
freeCodeCamp
forum.freecodecamp.org › python
Navigating and extracting XML in Python - Python - The freeCodeCamp Forum
February 8, 2023 - Hi, I’m trying to find a way to extract certain elements from the note data in this xml document: F 1 4 3 1 half I now need the data from the tied element, and whilst extracting the data from pitch, I had no problem, as I simply itterated through pitch as follows: for pitch in myroot.ite...
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-specific-nodes-in-xml-file-in-python
How to get specific nodes in xml file in Python?
September 1, 2025 - All book titles: - Python Programming ... for single elements, findall() with XPath for attribute filtering, and iter() for collecting all elements by tag name....
Find elsewhere
🌐
Linux Manual Page
linux.die.net › diveintopython › html › xml_processing › attributes.html
9.6. Accessing element attributes
>>> xmldoc = minidom.parse('binary.xml') >>> reflist = xmldoc.getElementsByTagName('ref') >>> bitref = reflist[0] >>> print bitref.toxml() <ref id="bit"> <p>0</p> <p>1</p> </ref> >>> bitref.attributes <xml.dom.minidom.NamedNodeMap instance at 0x81e0c9c> >>> bitref.attributes.keys() [u'id'] >>> bitref.attributes.values() [<xml.dom.minidom.Attr instance at 0x81d5044>] >>> bitref.attributes["id"] <xml.dom.minidom.Attr instance at 0x81d5044>
🌐
The Coding Forums
thecodingforums.com › archive › archive › python
finding element by tag in xml | Python | Coding Forums
February 20, 2010 - You are mixing two different types of xml parsers, either user minidom for node in dom.getElementsByTagName('author'): print node.getElementsByTagName('book') or use etree for el in items: if el.tag == 'author': print el.find('book') There are a few differences you need to note, the getElementsByTagName always returns a list even if there is only 1 element and find only returns the first element found no matter what.
Top answer
1 of 2
2

Since the latter approach resulted into broken mismatched pairs, looks like there are codes with no descriptions.

If you want to capture only the diagnosis codes that have descriptions (existing desc nodes), you can enforce this rule with the following XPath expression:

.//diag[name and desc]

The problem though is that xml.etree.ElementTree supports a limited set of XPath features and for this particular expression to work, you need to switch to lxml.etree. But, it will come with a performance boost, better memory usage and a richer functionality. It's worth it.

You can also simplify the way you extract codes by using findtext() and a dictionary comprehension:

from pprint import pprint

import lxml.etree as ET


data = """your XML here"""
root = ET.fromstring(data)

result = {diag.findtext("name"): diag.findtext("desc")
          for diag in root.xpath(".//diag[name and desc]")}

pprint(result)
2 of 2
2

XPath support fetching parent element https://docs.python.org/2/library/xml.etree.elementtree.html#supported-xpath-syntax. Let's try to find name element and get parent of this element:

>>> [x for x in tree.findall('.//name/..')]
[<Element 'diag' at 0x7f9c8cece278>,
 <Element 'diag' at 0x7f9c85a84908>,
 <Element 'diag' at 0x7f9c85a93f98>,
 <Element 'diag' at 0x7f9c85a8f188>]

When we have parent element, we can get name and desc elements:

>>> [(x.find('name'), x.find('desc')) for x in tree.findall('.//name/..')]
[(<Element 'name' at 0x7f9c875ba7c8>, <Element 'desc' at 0x7f9c8cedfe58>),
 (<Element 'name' at 0x7f9c85a84958>, <Element 'desc' at 0x7f9c85a84f98>),
 (<Element 'name' at 0x7f9c85a8f048>, <Element 'desc' at 0x7f9c85a8f098>),
 (<Element 'name' at 0x7f9c85a8f1d8>, <Element 'desc' at 0x7f9c85a8f228>)]

And finally:

>>> [(x.find('name').text, x.find('desc').text) for x in tree.findall('.//name/..')]
[('A00', 'Cholera'),
 ('A00.0', 'Cholera due to Vibrio cholerae 01, biovar cholerae'), 
 ('A00.1', 'Cholera due to Vibrio cholerae 01, biovar eltor'), 
 ('A00.9', 'Cholera, unspecified')]
🌐
W3Schools
w3schools.com › xml › met_document_getelementsbytagname.asp
XML DOM getElementsByTagName() Method
DOM Node Types DOM Node DOM NodeList DOM NamedNodeMap DOM Document DOM Element DOM Attribute DOM Text DOM CDATA DOM Comment DOM XMLHttpRequest DOM Parser XSLT Elements XSLT/XPath Functions
🌐
Python.org
discuss.python.org › python help
Extract information from an xml file - Python Help - Discussions on Python.org
June 12, 2023 - I have an xml file looks like this <report> #root element <bindingsite id="1" has_interactions="True"> <interactions> #child elements <hydrophobic_interactions/> #the sub-child elements I want to get name …
🌐
OMZ Software
omz-software.com › editorial › docs › library › xml.etree.elementtree.html
19.7. xml.etree.ElementTree — The ElementTree XML API — Editorial Documentation
Finds all matching subelements, by tag name or path. Same as getroot().iterfind(match). Returns an iterable yielding all matching elements in document order. New in version 2.7. ... Loads an external XML section into this element tree. source is a file name or file object.
🌐
Medium
medium.com › @franksands › searching-an-xml-in-python-77d8028dea42
Searching an xml in Python. Starting with version 2.7 ElementTree… | by Francisco Areas Guimaraes | Medium
September 1, 2017 - Starting with version 2.7 ElementTree has a better support for XPath queries. XPath is a syntax to enable you to navigate through an xml like SQL is used to search through a database. Both find and findallfunctions support XPath.
🌐
Python Forum
python-forum.io › thread-15453.html
Using xpath to get value of a nested element name using tag named
I have a nested XML tag file that parses correctly with elementtree. I have been unable to find a method of getting the text value for an element nested in the file. If I do a for loop I can do a findall and retrieve my values but would like to use...
🌐
TutorialsPoint
tutorialspoint.com › article › get-tag-name-using-beautifulsoup-in-python
Get tag name using Beautifulsoup in Python
March 27, 2026 - from bs4 import BeautifulSoup # HTML document to be parsed html_doc = """ <html> <head> <title>TutorialsPoint</title> </head> <body> <p>TutorialsPoint</p> </body> </html> """ # Parse the HTML document using BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser') # Get the first <p> tag in the HTML document p_tag = soup.find('p') # Get the tag name using the name attribute tag_name = p_tag.name # Print the tag name print("Tag name is:", tag_name) ... from bs4 import BeautifulSoup xml_doc = ''' <book> <title>Harry Potter</title> <author>J.K.
🌐
Kite
kite.com › python › examples › 3450 › xml-get-the-tag-name-of-an-xml-element
Kite is saying farewell - Code Faster with Kite
November 20, 2022 - P.S. Most of our code has been open sourced on Github here. It includes our data-driven Python type inference engine, Python public-package analyzer, desktop software, editor integrations, Github crawler and analyzer, and much more.
🌐
Apify
blog.apify.com › python-parse-xml
How to parse XML in Python
July 16, 2025 - The root.iter("author") returns an iterator that yields all elements with the specified tag name ("author" in this case). import xml.etree.ElementTree as ET tree = ET.parse("bookstore_data.xml") root = tree.getroot() for auth in root.iter("author"): print(auth.text)