The namespace of an XML document is significant. ElementTree requires tags to be fully qualified to find the right element. Here's an example of three elements with the same tag in different namespaces:

data = '''\
<root xmlns="xyz" xmlns:name="abc">
  <object name="one" />
  <name:object name="two" />
  <object xmlns="def" name="three" />
</root>
'''

Here's the elements that ElementTree sees:

>>> from xml.etree import ElementTree as et
>>> tree = et.fromstring(data)
>>> print(tree.findall('.//*'))
>>> et.dump(tree)
[<Element '{xyz}object' at 0x0000000003B07BD8>,
 <Element '{abc}object' at 0x0000000003B07C28>,
 <Element '{def}object' at 0x0000000003B07C78>]

So you have it right. Given the default namespace definition of:

<entry xmlns='http://www.w3.org/2005/Atom' ...

To access the 'title' tag, which uses the default namespace:

media['title'] = e.findall('{http://www.w3.org/2005/Atom}title')

to access the 'media:group' tag, refer to the media namespace definition:

<entry ... xmlns:media='http://search.yahoo.com/mrss/' ...

And use:

e.findall('{http://search.yahoo.com/mrss/}group')

Note the different ways a namespace can be specified:

<root xmlns="xyz" xmlns:name="abc">   # default namespace and
                                      # 'abc' namespace with id 'name'.
  <object name="one" />               # Uses default namespace 'xyz'.
  <name:object name="two" />          # uses 'abc' namespace (specified by id).
  <object xmlns="def" name="three" /> # change the default namespace to 'def'.
</root>

To read a specific tag from a specific namespace:

>>> print(tree.find('{abc}object').attrib['name'])
'two'

Note the namespace IDs are just shortcuts. Here's what happens when you dump the parsed XML tree. ElementTree doesn't bother to save the original namespace IDs and generates its own in the format ns#:

>>> et.dump(tree)
<ns0:root xmlns:ns0="xyz" xmlns:ns1="abc" xmlns:ns2="def">
  <ns0:object name="one" />
  <ns1:object name="two" />
  <ns2:object name="three" />
</ns0:root>

If you want specific shortcuts defined, use `register_namespace':

>>> et.register_namespace('','xyz') # default namespace
>>> et.register_namespace('name','abc')
>>> et.register_namespace('custom','def')
>>> et.dump(tree)
<root xmlns="xyz" xmlns:custom="def" xmlns:name="abc">
  <object name="one" />
  <name:object name="two" />
  <custom:object name="three" />
</root>
Answer from Mark Tolonen on Stack Overflow
Top answer
1 of 2
15

The namespace of an XML document is significant. ElementTree requires tags to be fully qualified to find the right element. Here's an example of three elements with the same tag in different namespaces:

data = '''\
<root xmlns="xyz" xmlns:name="abc">
  <object name="one" />
  <name:object name="two" />
  <object xmlns="def" name="three" />
</root>
'''

Here's the elements that ElementTree sees:

>>> from xml.etree import ElementTree as et
>>> tree = et.fromstring(data)
>>> print(tree.findall('.//*'))
>>> et.dump(tree)
[<Element '{xyz}object' at 0x0000000003B07BD8>,
 <Element '{abc}object' at 0x0000000003B07C28>,
 <Element '{def}object' at 0x0000000003B07C78>]

So you have it right. Given the default namespace definition of:

<entry xmlns='http://www.w3.org/2005/Atom' ...

To access the 'title' tag, which uses the default namespace:

media['title'] = e.findall('{http://www.w3.org/2005/Atom}title')

to access the 'media:group' tag, refer to the media namespace definition:

<entry ... xmlns:media='http://search.yahoo.com/mrss/' ...

And use:

e.findall('{http://search.yahoo.com/mrss/}group')

Note the different ways a namespace can be specified:

<root xmlns="xyz" xmlns:name="abc">   # default namespace and
                                      # 'abc' namespace with id 'name'.
  <object name="one" />               # Uses default namespace 'xyz'.
  <name:object name="two" />          # uses 'abc' namespace (specified by id).
  <object xmlns="def" name="three" /> # change the default namespace to 'def'.
</root>

To read a specific tag from a specific namespace:

>>> print(tree.find('{abc}object').attrib['name'])
'two'

Note the namespace IDs are just shortcuts. Here's what happens when you dump the parsed XML tree. ElementTree doesn't bother to save the original namespace IDs and generates its own in the format ns#:

>>> et.dump(tree)
<ns0:root xmlns:ns0="xyz" xmlns:ns1="abc" xmlns:ns2="def">
  <ns0:object name="one" />
  <ns1:object name="two" />
  <ns2:object name="three" />
</ns0:root>

If you want specific shortcuts defined, use `register_namespace':

>>> et.register_namespace('','xyz') # default namespace
>>> et.register_namespace('name','abc')
>>> et.register_namespace('custom','def')
>>> et.dump(tree)
<root xmlns="xyz" xmlns:custom="def" xmlns:name="abc">
  <object name="one" />
  <name:object name="two" />
  <custom:object name="three" />
</root>
2 of 2
5

Actually I have tried the following way using xml.dom.minidom, Just in case it helps you anyway.

#!/usr/bin/python

from xml.dom.minidom import parseString
import re
import urllib

def get_video_id(video_url):
    return re.search(r'watch\?v=.*', video_url).group(0)[8:]

def get_video_feed(video_url):
    video_feed = "http://gdata.youtube.com/feeds/api/videos/" + get_video_id(video_url)
    print video_feed
    return urllib.urlopen(video_feed).read()

def get_media_info(video_url):
    content = get_video_feed(video_url)
    dom = parseString(content)
    media = {}

    media['title'] = dom.getElementsByTagName('title')[0].firstChild.nodeValue
    return media

def main():
    video_url = 'http://youtube.com/watch?v=q5sOLzEerwA' 

    print ( get_media_info(video_url) )

if __name__ == '__main__':
    main()
🌐
TutorialsPoint
tutorialspoint.com › How-to-get-specific-nodes-in-xml-file-in-Python
How to get specific nodes in xml file in Python?
In some XML files, elements contains additional information which stored as attributes. These attributes are used to identify and filter the elements we want and this can be performed by using the findall() method in Python. Following is the example, in which we get the book by its id from ...
Discussions

xml - Python getting element value for specific element - Stack Overflow
I am attempting to parse the below XML file but having difficulty getting a specific element value. I am trying to specify element 'Item_No_2' to get the related value 2222222222 ... More on stackoverflow.com
🌐 stackoverflow.com
August 23, 2016
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
Xml - Find Element By tag using Python - Stack Overflow
I am trying extract some data from a bunch of xml files. Now, the issue is the structure of all the files is not exactly the same and thus, just iterating over the children and extracting the value... More on stackoverflow.com
🌐 stackoverflow.com
parse a specific element in a xml file using Python - Stack Overflow
It would be still better if you stripped down the XML even further, and embedded it as a triple-quoted string in your sample code, so we could just copy and paste your example into a Python interpreter to make it easier to debug. But good enough for a +1 from me. ... So, what's going on? You don't have any elements ... More on stackoverflow.com
🌐 stackoverflow.com
June 6, 2017
🌐
Stack Overflow
stackoverflow.com › questions › 58622395 › get-element-value-from-xml-file-using-python
Get element value from xml file using Python - Stack Overflow
import xml.etree.ElementTree as ET tree = ET.parse('csr1kv_file.xml') root = tree.getroot() ET.register_namespace("","http://www.test.com/esc/esc") for subnet in root.iter('address'): print (subnet)
🌐
freeCodeCamp
forum.freecodecamp.org › programming › 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...
🌐
Rdegges
rdegges.com › 2013 › quickly-extract-xml-data-with-python
Randall Degges - Quickly Extract XML Data with Python
Load our XML document into memory, and construct an XML ElementTree object. We then use the find method, passing in an XPath selector, which allows us to specify what element we’re trying to extract.
🌐
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
Their values are usually strings but may be any application-specific object. If the element is created from an XML file, the text attribute holds either the text between the element’s start tag and its first child or end tag, or None, and the tail attribute holds either the text between the element’s end tag and the next tag, or None.
Find elsewhere
🌐
AskPython
askpython.com › home › python xml parser
Python XML Parser - AskPython
July 7, 2022 - The syntax is similar to our xml module, so we’re still getting the attribute names using value = tag['attribute_name'] and text = tag.text. Exactly the same as before! ... We’ve now parsed this using bs4 too! If your source XML file is badly formatted, this method is the way to go since BeautifulSoup has different rules for handling such files. Hopefully, you’ve now got a good grasp on how to build a Python XML parser easily.
🌐
Zyte
zyte.com › home › resources › learn › a practical guide to python xml parsing
A Practical Guide to Python XML Parsing
May 15, 2025 - While basic parsing and modification techniques cover most use cases, more complex scenarios may require advanced techniques like XPath queries, handling XML namespaces, and mapping XML data to custom Python objects. XPath is a powerful query language for selecting nodes in an XML document based on various criteria. Here’s how to use XPath to find specific elements in an XML document:
🌐
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 …
🌐
Reddit
reddit.com › r/learnpython › find element whose attribute contains a value in xml.etree?
r/learnpython on Reddit: Find element whose attribute contains a value in xml.etree?
May 23, 2020 -

How would I go about finding all elements whose attribute 'id' contain a certain value?

e.g., the id's values vary and can be e.g. 'foo 1' 'foo 23' 'foo 9'

How do I go about getting all the elements whose id contains the word foo disregarding whichever number it is followed by?

🌐
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!
🌐
Kite
kite.com › python › answers › how-to-get-elements-by-tag-in-an-xml-file-in-python
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.