Parsing XML root attributes
Published: Wednesday, May 27, 2009 Last modified: Friday, May 12, 2023
Imagine you want to read the attribute test= from this fragment of XML called
<widget test="foo"> <name>blah</name> </widget>
Using lxml, you need to run getroot.
import lxml.etree as etree tree = etree.parse("test.xml") print(tree.getroot().attrib['test'])