How to extract xml attribute using Python ElementTree

PythonXmlXpathElementtree

Python Problem Overview


For:

<foo>
 <bar key="value">text</bar>
</foo>

How do I get "value"?

xml.findtext("./bar[@key]")

Throws an error.

Python Solutions


Solution 1 - Python

This will find the first instance of an element named bar and return the value of the attribute key.

In [52]: import xml.etree.ElementTree as ET

In [53]: xml=ET.fromstring(contents)

In [54]: xml.find('./bar').attrib['key']
Out[54]: 'value'

Solution 2 - Python

Getting child tag's attribute value in a XML using ElementTree

Parse the XML file and get the root tag and then using [0] will give us first child tag. Similarly [1], [2] gives us subsequent child tags. After getting child tag use .attrib[attribute_name] to get value of that attribute.

>>> import xml.etree.ElementTree as ET
>>> xmlstr = '<foo><bar key="value">text</bar></foo>'
>>> root = ET.fromstring(xmlstr)
>>> root.tag
'foo'
>>> root[0].tag
'bar'
>>> root[0].attrib['key']
'value'

If the xml content is in file. You should do below task to get the root.

>>> tree = ET.parse('file.xml')
>>> root = tree.getroot()

Solution 3 - Python

Your expression:

> ./bar[@key]

It means: bar children having key attribute

If you want to select the attribute, use this relative expression:

bar/@key

It means: the key attribute of bar children

Of course, you need to consider to use a fully compliant XPath engine like lxml.

Solution 4 - Python

By following method you can get all attributes from xml (in Dictionary)

import xml.etree.ElementTree as etree
xmlString= "<feed xml:lang='en'><title>World Wide Web</title><subtitle lang='en'>Programming challenges</subtitle><link rel='alternate' type='text/html' href='http://google.com/'/><updated>2019-12-25T12:00:00</updated></feed>"
xml= etree.fromstring(xmlString)  

def get_attr(xml):
    attributes = []
    for child in (xml):
        if len(child.attrib)!= 0:
            attributes.append(child.attrib)
        get_attr(child)
    return attributes
attributes = get_attr(xml)

print(attributes)

Solution 5 - Python

dipenparmar12 function will not return the childrens child attributes. Because the function is recursive the attributes list will be set to a empty list for each call. This function will has return the childrens child.

import xml.etree.ElementTree as etree
xml= etree.fromstring(xmlString) 


 def get_attr(xml, attributes):
     for child in (xml):
         if len(child.attrib)!= 0:
             attributes.append(child.attrib)
         get_attr(child,attributes)
     return attributes

  attributes = get_attr(xml,[])
  print(attributes)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWill CurranView Question on Stackoverflow
Solution 1 - PythonunutbuView Answer on Stackoverflow
Solution 2 - PythonrashokView Answer on Stackoverflow
Solution 3 - Pythonuser357812View Answer on Stackoverflow
Solution 4 - Pythondipenparmar12View Answer on Stackoverflow
Solution 5 - PythonmökenView Answer on Stackoverflow