emre5
emre5

Reputation: 31

Get attribute of first element using lxml

Trying to parse an XML file using lxml in Python, how do I simply get the value of an element's attribute? Example:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<item id="123">
    <sub>ABC</sub>
</item>

I'd like to get the result 123, and store it as a variable.

Upvotes: 1

Views: 3297

Answers (3)

Amjad
Amjad

Reputation: 53

I think Martijn has answered your question. Building on his answer, you can also use the items() method to get a list of tuples with the attributes and values. This may be useful if you need the values of multiple attributes. Like so:

>>> from lxml import etree
>>> tree = etree.parse('test.xml')
>>> item = tree.xpath('/item')
>>> item.items()
[('id', '123')]

Or in case of string:

>>> tree = etree.fromstring("""\
... <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
... <item id="123">
...     <sub>ABC</sub>
... </item>
... """)
>>> tree.items()
[('id', '123')]

Upvotes: 0

senshin
senshin

Reputation: 10360

Alternatively, you could use an XPath selector:

>>> from lxml import etree
>>> tree = etree.fromstring(b'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<item id="123">
    <sub>ABC</sub>
</item>''')
>>> tree.xpath('/item/@id')
['123']

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1124548

When using etree.parse(), simply call .getroot() to get the root element; the .attrib attribute is a dictionary of all attributes, use that to get the value:

>>> from lxml import etree
>>> tree = etree.parse('test.xml')
>>> tree.getroot().attrib['id']
'123'

If you used etree.fromstring() the object returned is the root object already, so no .getroot() call is needed:

>>> tree = etree.fromstring('''\
... <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
... <item id="123">
...     <sub>ABC</sub>
... </item>
... ''')
>>> tree.attrib['id']
'123'

Upvotes: 2

Related Questions