> here is a simplified version of an xml file: > > <?xml version="1.0" encoding="UTF-8"?> > <gpx > > <metadata> > <author> > <name>CloudMade</name> > <email id="support" domain="cloudmade.com" /> > <link href="http://maps.cloudmade.com"></link> > </author> > <copyright author="CloudMade"> > <license>http://cloudmade.com/faq#license</license> > </copyright> > <time>2011-07-28T07:04:01</time> > </metadata> > <extensions> > <distance>1489</distance> > <time>344</time> > <start>Sägerstraße</start> > <end>Im Gisinger Feld</end> > </extensions> > </gpx> > > I want to get the value of the distance element - 1489. What is the > simplest way of doing this?
#!/usr/bin/env python # -*- coding: utf-8 -*- from xml.etree.ElementTree import fromstring data = """<?xml version="1.0" encoding="UTF-8"?> <gpx > <metadata> <author> <name>CloudMade</name> <email id="support" domain="cloudmade.com" /> <link href="http://maps.cloudmade.com"></link> </author> <copyright author="CloudMade"> <license>http://cloudmade.com/faq#license</license> </copyright> <time>2011-07-28T07:04:01</time> </metadata> <extensions> <distance>1489</distance> <time>344</time> <start>Sägerstraße</start> <end>Im Gisinger Feld</end> </extensions> </gpx>""" def parse_xml(s): element = fromstring(s) return element.find("extensions/distance").text if __name__ == "__main__": print parse_xml(data) Hope that helps. Regards, BG -- Baishampayan Ghose b.ghose at gmail.com _______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers