【问题标题】:How to find element in KML tree containing ':'如何在 KML 树中查找包含“:”的元素
【发布时间】:2012-03-14 11:12:21
【问题描述】:

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

代码:

#!/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")

示例数据文件:

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

错误:

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 (:)

代码适用于没有':'的元素

【问题讨论】:

    标签: python xml kml elementtree


    【解决方案1】:

    gx{http://www.google.com/kml/ext/2.2} 的命名空间快捷方式。从find 中取出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)
    

    输出

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

    【讨论】:

      【解决方案2】:

      我知道这是使用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']
      

      我相信类似的方法可以用于ElementTree

      【讨论】:

        猜你喜欢
        • 2013-04-17
        • 2016-11-03
        • 2020-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-16
        • 2022-06-23
        相关资源
        最近更新 更多