Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在包含';的KML树中查找元素:';_Python_Xml_Kml_Elementtree - Fatal编程技术网

Python 如何在包含';的KML树中查找元素:';

Python 如何在包含';的KML树中查找元素:';,python,xml,kml,elementtree,Python,Xml,Kml,Elementtree,问题:找不到带有“:”的元素-无法运行程序。我找到了一些关于的参考资料,但我不知道如何应用它们 代码: 示例数据文件: <?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

问题:找不到带有“:”的元素-无法运行程序。我找到了一些关于的参考资料,但我不知道如何应用它们

代码:

示例数据文件:

<?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>

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
错误:

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 (:)
回溯(最近一次呼叫最后一次):
文件“main.py”,第7行,在
track=kmlTree.find(“)//{http://www.opengis.net/kml/2.2}gx:Track”)#最有趣的数据存储在此标记中
文件“/usr/lib/python2.6/xml/etree/ElementTree.py”,第614行,在find中
返回self.\u root.find(路径)
文件“/usr/lib/python2.6/xml/etree/ElementTree.py”,第330行,在find中
return ElementPath.find(self,path)
文件“/usr/lib/python2.6/xml/etree/ElementPath.py”,第186行,在find中
return _compile(path).find(element)
文件“/usr/lib/python2.6/xml/etree/ElementPath.py”,第176行,在编译中
p=路径(路径)
文件“/usr/lib/python2.6/xml/etree/ElementPath.py”,第93行,在__
“预期的路径分隔符(%s)”%(操作或标记)
SyntaxError:应为路径分隔符(:)

代码正在为没有“:”的元素工作。

我知道这是一个使用的替代解决方案

>tree=etree.parse('test.xml'))
>>>xpath(“.//gx:Track”,名称空间={'gx':'http://www.google.com/kml/ext/2.2'})
[]
>>>xpath(“//gx:Track/*/text()”,名称空间={'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']

我相信类似的方法也可以用于
ElementTree
gx
{http://www.google.com/kml/ext/2.2}
。从
查找
中取出
gx:
,并使用正确的命名空间:

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)
从xml.etree导入ElementTree作为et
数据=“”\
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
'''
kmlTree=et.fromstring(数据)
track=kmlTree.find(“)//{http://www.google.com/kml/ext/2.2}轨道“)
打印(轨道)
输出

>>> 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']
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)
<Element '{http://www.google.com/kml/ext/2.2}Track' at 0x40cca70>