How to find an element in the KML tree containing ':'

Problem: cannot find element with ':' - cannot start program. I found some links about Qualified Names , but I do not know how to apply them.

the code:

#!/usr/bin/env python from xml.etree.ElementTree import ElementTree kmlTree = ElementTree() kmlTree.parse("data/history-03-08-2012.kml") track = kmlTree.find(".//{http://www.opengis.net/kml/2.2}gx:Track") 

Example data file:

 <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"> <gx:Track> <when>2012-03-10T05:52:38.564-08:00</when> <gx:coord>16.424247 48.236804 0</gx:coord> <when>2012-03-10T06:00:39.748-08:00</when> <gx:coord>16.424247 48.236804 0</gx:coord> </gx:Track> </kml> 

Error:

 Traceback (most recent call last): File "main.py", line 7, in <module> track = kmlTree.find(".//{http://www.opengis.net/kml/2.2}gx:Track") #most interesting data is stored in this tag File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 614, in find return self._root.find(path) File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 330, in find return ElementPath.find(self, path) File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 186, in find return _compile(path).find(element) File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 176, in _compile p = Path(path) File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 93, in __init__ "expected path separator (%s)" % (op or tag) SyntaxError: expected path separator (:) 

The code works for elements without ':'

+4
source share
2 answers

gx is the namespace shortcut for {http://www.google.com/kml/ext/2.2} . Take gx: from find and use the correct namespace:

 from xml.etree import ElementTree as et data = '''\ <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"> <gx:Track> <when>2012-03-10T05:52:38.564-08:00</when> <gx:coord>16.424247 48.236804 0</gx:coord> <when>2012-03-10T06:00:39.748-08:00</when> <gx:coord>16.424247 48.236804 0</gx:coord> </gx:Track> </kml> ''' kmlTree = et.fromstring(data) track = kmlTree.find(".//{http://www.google.com/kml/ext/2.2}Track") print(track) 

Exit

 <Element '{http://www.google.com/kml/ext/2.2}Track' at 0x40cca70> 
+4
source

I know this is an alternative solution that uses lxml .

 >>> tree = etree.parse('test.xml') >>> tree.xpath(".//gx:Track",namespaces={'gx':'http://www.google.com/kml/ext/2.2'}) [<Element {http://www.google.com/kml/ext/2.2}Track at 0x1c1e3f0>] >>> tree.xpath("//gx:Track/*/text()",namespaces={'gx':'http://www.google.com/kml/ext/2.2'}) ['2012-03-10T05:52:38.564-08:00', '16.424247 48.236804 0', '2012-03-10T06:00:39.748-08:00', '16.424247 48.236804 0'] 

I believe that a similar approach can be used for ElementTree .

0
source

Source: https://habr.com/ru/post/1401352/


All Articles