ElementTree - Howto access text within XML tag element...

Stefan Behnel stefan_ml at behnel.de
Wed Aug 12 09:18:03 CEST 2009


cmalmqui wrote:
> tree = etree.parse('10_07_2009 16_48_00_history.tcx')
> root = tree.getroot()
> 
> elem = root[0][0]
> 
> # iterate over all laps
> for i in range(1, len(elem)-1):

Note that you can iterate over elements as in

	for lap_element in elem:
	    # ...

Then use

	record = lap.find("recordtagname")

to find things inside the subtree. You can also use XPath-like expressions
such as

	all_intersting_elements =
		lap.findall("sometag/somechild//somedescendant")

Stefan



More information about the Python-list mailing list