Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the root node of an xml file in Python?

Tags:

python

xml

Basically I am using:

from xml.etree import ElementTree as ET

path = 'C:\cool.xml'
et = ET.parse ( path )

But I am not sure how to get the root from et?

like image 511
Joan Venge Avatar asked Dec 05 '22 06:12

Joan Venge


2 Answers

You probably want:

et.getroot()

Have a look at the official docs for ElementTree from the effbot site. Note that Python 2.5 (the first version of Python to include ElementTree out of the box) uses ElementTree 1.2, not the more recent 1.3. There aren't many differences, but just FYI in case.

like image 67
Jarret Hardie Avatar answered Dec 08 '22 03:12

Jarret Hardie


root = et.getroot()

I would recommend using lxml.etree instead of xml.etree.ElementTree, as lxml is faster and the interface is the same.

like image 31
rmmh Avatar answered Dec 08 '22 03:12

rmmh