How to extract xml attribute using Python ElementTree
Asked Answered
C

6

71

For:

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

How do I get "value"?

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

Throws an error.

Crock answered 1/1, 2011 at 5:16 Comment(0)
T
88

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'
Tiein answered 1/1, 2011 at 5:56 Comment(1)
@Stevoisiak, i have similar problem <image height="940" id="0" name="C02032-390.jpg" width="1820"> <box label="Objects" occluded="1" xbr="255" xtl="0" ybr="624" ytl="509"> <attribute name="Class">Car</attribute> </box> </image> I would like to access "Car" from the attribute.Kinematograph
F
12

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()
Fingerprint answered 26/4, 2018 at 14:25 Comment(1)
I get error saying AttributeError: 'builtin_function_or_method' object has no attribute 'fromstring'Conservancy
G
3

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)
Gristmill answered 18/5, 2019 at 14:24 Comment(0)
B
0

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.

Betty answered 1/1, 2011 at 16:56 Comment(4)
Not sure if it's ElementTree or Google App Engine but the use of '@' raises SyntaxError("unsupported path syntax (%s)" % op) SyntaxError: unsupported path syntax (@)Crock
@Will Merydith: Please, read my last sentence. Basic ElementTree API it's not a full complain XPath engine...Betty
OK. I'll see if I can find a module that will work on GAE/Py2.5.5.Crock
It seems that python ElementTree does not support syntax like bar/@key and you have to use xxx.attribut.get("key") for the appropriate xxx.Bugbear
F
0

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)
Fivespot answered 8/1, 2021 at 13:51 Comment(0)
R
-1

For going deeper into the tree this type of functions can be used.

root[1][2][0].tag  # For displaying the nodes
root[1][2][0].text # For showing what's inside the node
Rew answered 24/6, 2022 at 11:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.