How to get root node xml file in Python?

I mainly use:

from xml.etree import ElementTree as ET

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

But I'm not sure how to get root from et?

+3
source share
3 answers

You probably want:

et.getroot()

Check out the official ElementTree docs on the effbot website . Note that Python 2.5 (the first version of Python to include ElementTree out of the box) uses ElementTree 1.2, not later 1.3. There are not many differences, but only FYI in case.

+10
source
root = et.getroot()

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

+4
source
root = et.getroot()
+2
source

Source: https://habr.com/ru/post/1705431/


All Articles