ElementTree - Howto access text within XML tag element...
cmalmqui
cmalmqui at gmail.com
Tue Aug 11 16:20:35 EDT 2009
On Aug 11, 9:51 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> cmalmqui schrieb:
> > Hi,
>
> > I am writing on a small XML parser and are currently stuck as I am not
> > able to get the whole element name in ElementTree.
>
> > Please see the below example where "print root[0][0]" returns
> > "<Element 'Activity' at 018A3938>"
>
> > Is there a way to get hold of the "Running" string in the tag using
> > elementTree?
>
> > <Activities>
> > <Activity Sport="Running">
> > <Id>2009-07-10T14:48:00Z</Id>
> > <Lap StartTime="2009-07-10T14:48:00Z">
> > .........
>
> > For those of you that know how to program XML I have another
> > question:
> > I am currently "hardcoding" my XML parser using brackets, is this a
> > good approach or should I build it using a "search on tag" approach.
>
> What do you mean by that - hardcoding by brackets?
>
> Diez
Indeed, my current approach has been to hardcode the XML parser using
brackets. Is there a more elegant way?
I am parsing a garmin xml file from a handheld GPS and as you can see
in the below script, I am hardcoding each node:
import xml.etree.cElementTree as etree
def gettext(elem):
text = elem.text or ""
for e in elem:
text += gettext(e)
if e.tail:
text += e.tail
return text
tree = etree.parse('10_07_2009 16_48_00_history.tcx')
root = tree.getroot()
elem = root[0][0]
# ID Tag
print "type of exercise : " + elem.get("Sport")
print "excercise starttime : " + gettext(elem[0])
# iterate over all laps
for i in range(1, len(elem)-1):
# LAP TAG
print "\nlap number : " + str(i)
print "lap start time : " + str(elem[i].get("StartTime"))
print "lap duration (s) : " + gettext(elem[i][0])
print "lap length (m) : " + gettext(elem[i][1])
print "max speed (km/h) : " + gettext(elem[i][2])
print "number of calories : " + gettext(elem[i][3])
print "average heartbeat : " + gettext(elem[i][4][0])
print "max heartbeat : " + gettext(elem[i][5][0])
print "number of records : " + str(len(elem[i][8])-1)
for j in range(1, len(elem[i][8])-1):
time = gettext(elem[i][8][j][0]) #time
lat = gettext(elem[i][8][j][1][0]) #lat
lon = gettext(elem[i][8][j][1][1]) #lon
alt = gettext(elem[i][8][j][2]) #alt
dist = gettext(elem[i][8][j][3]) #distance from start
bpm = gettext(elem[i][8][j][4][0]) #beats per minute
#print time + " " + lat + " " + lon + " " + alt + " " + dist +
" " + bpm
print "\nReceiver Info : " + gettext(elem[len(elem)-1][0])
More information about the Python-list
mailing list