Elementtree find problem

Larry Bates larry.bates at websafe.com
Tue Dec 11 18:30:44 EST 2007


Willemsjunk at gmail.com wrote:
> I tried the tips I found in other posts but I still get 'none' back:
> 
> import easygui as eg
> import xml.etree.ElementTree as ET
> import sys
> 
> #kml source is:
> #<?xml version="1.0" encoding="UTF-8"?>
> #<kml xmlns="http://earth.google.com/kml/2.2">
> #  <Placemark>
> #    <name>Simple placemark</name>
> #    <description>Attached to the ground. Intelligently places itself
> #       at the height of the underlying terrain.</description>
> #    <Point>
> #      <coordinates>-122.0822035425683,37.42228990140251,0</
> coordinates>
> #    </Point>
> #  </Placemark>
> #</kml>
> 
> 
> #select a file and open it in python
> f_name=eg.fileopenbox("select kml file", "Select kml file")
> f_kml=open(f_name,'r')
> 
> #parse and create Elementtree and go to root
> tree=ET.parse(f_kml)
> kml=tree.getroot()
> 
> #look for description
> coord= kml.find('.//description')
> print coord
> 
> 
> _______________________
> 
> the result is 'None' ...
> 
> If I print out the kml with tostring I can see the entire file and to
> me the XPath string also seems correct
> 
> any idea?

This works:

 >>> url='http://earth.google.com/kml/2.2'
 >>> coord=kml.find('{%s}Placemark/{%s}Point/{%s}coordinates' % \
                     (url, url, url)).text
 >>> print coord
'-122.0822035425683,37.42228990140251,0'

-Larry



More information about the Python-list mailing list