I want to find all the Placemarks in a kml file:
JavaScript
x
5
1
from lxml import etree
2
doc = etree.parse(filename)
3
for elem in doc.findall('<Placemark>'):
4
print(elem.find("<Placemark>").text)
5
This doesn’t work, i.e. it doesn’t find anything, I think because each Placemark is unique in that each has its own id, e.g.:
JavaScript
1
4
1
<Placemark id="ID_09795">
2
<Placemark id="ID_15356">
3
<Placemark id="ID_64532">
4
How do I do this?
Edit: changed code based on @ScottHunter comment:
JavaScript
1
5
1
placemark_list = doc.findall("Placemark")
2
print ("length:" + str(len(placemark_list)))
3
for placemark in placemark_list:
4
print(placemark.text)
5
length is 0
Advertisement
Answer
It’s hard to tell without seeing the full file, but try something like this
JavaScript
1
3
1
placemark_list = doc.xpath("//*[local-name()='Placemark']")
2
print(len(placemark_list))
3
and see if it works.