Joan Venge
Joan Venge

Reputation: 330942

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

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?

Upvotes: 5

Views: 2945

Answers (3)

Jarret Hardie
Jarret Hardie

Reputation: 97912

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.

Upvotes: 11

rmmh
rmmh

Reputation: 7095

root = et.getroot()

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

Upvotes: 4

Jason Coon
Jason Coon

Reputation: 18421

root = et.getroot()

Upvotes: 2

Related Questions