xml - Python ElementTree find() not matching within kml file -
i'm trying find element kml file using element trees follows:
from xml.etree.elementtree import elementtree tree = elementtree() tree.parse("history-03-02-2012.kml") p = tree.find(".//name")
a sufficient subset of file demonstrate problem follows:
<?xml version="1.0" encoding="utf-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <document> <name>location history 03/03/2012 03/10/2012</name> </document> </kml>
a "name" element exists; why search come empty?
the name
element you're trying match within kml namespace, aren't searching namespace in mind.
try:
p = tree.find(".//{http://www.opengis.net/kml/2.2}name")
if using lxml's xpath instead of standard-library elementtree, you'd instead pass namespace in dictionary:
>>> tree = lxml.etree.fromstring('''<kml xmlns="http://www.opengis.net/kml/2.2"> ... <document> ... <name>location history 03/03/2012 03/10/2012</name> ... </document> ... </kml>''') >>> tree.xpath('//kml:name', namespaces={'kml': "http://www.opengis.net/kml/2.2"}) [<element {http://www.opengis.net/kml/2.2}name @ 0x23afe60>]
Comments
Post a Comment