使用pykml解析kml时出现Lxml错误

时间:2016-06-23 09:11:44

标签: python parsing lxml kml

我试图使用pykml解析包含多个地标的kml文件。我想编辑kml描述中的HTML代码,主要是为了Google Earth中地理数据的可视化目的。我已经研究了很多方法:

但是,我总是得到如下所示的lxml错误。 :(

    Traceback (most recent call last):
    File "C:\Users\Arellano\Copy\BSGE\2015-2016 SUMMER\trial7.py", line 5, in <module>
    root = parser.fromstring(open('trim_KML.kml', 'r').read())
  File "C:\Program Files (x86)\Python2.7.10\lib\site-packages\pykml-0.1.0-py2.7.egg\pykml\parser.py", line 41, in fromstring
    return objectify.fromstring(text)
  File "src/lxml/lxml.objectify.pyx", line 1801, in lxml.objectify.fromstring (src\lxml\lxml.objectify.c:25171)
  File "src/lxml/lxml.etree.pyx", line 3213, in lxml.etree.fromstring (src\lxml\lxml.etree.c:77697)
  File "src/lxml/parser.pxi", line 1819, in lxml.etree._parseMemoryDocument (src\lxml\lxml.etree.c:116494)
  File "src/lxml/parser.pxi", line 1707, in lxml.etree._parseDoc (src\lxml\lxml.etree.c:115144)
  File "src/lxml/parser.pxi", line 1079, in lxml.etree._BaseParser._parseDoc (src\lxml\lxml.etree.c:109543)
  File "src/lxml/parser.pxi", line 573, in lxml.etree._ParserContext._handleParseResultDoc (src\lxml\lxml.etree.c:103404)
  File "src/lxml/parser.pxi", line 683, in lxml.etree._handleParseResult (src\lxml\lxml.etree.c:105058)
  File "src/lxml/parser.pxi", line 613, in lxml.etree._raiseParseError (src\lxml\lxml.etree.c:103967)
XMLSyntaxError: Namespace prefix xsi for schemaLocation on Document is not defined, line 3, column 32

这是我的代码片段:(应该根据我的某个来源工作)

from pykml import parser

root = parser.fromstring(open('trim_KML.kml', 'r').read())
print etree.tostring(root.Document.Placemark.LineString.Description)

我已经安装了pykml和lxml 3.6.0,我正在使用我的Python 2.7.10。 kml文件包含行。 (kml链接:https://sites.google.com/site/kmlhostingmwss/trim.kml) 我的ArcGIS 10.2也有Python 2.7。

我是处理kml文件的新手。有人可以告诉我,我做错了什么?或者是否有更简单的方法来编辑kml文件的描述?非常感谢你。 :)))

1 个答案:

答案 0 :(得分:2)

xml存在一些问题,如果要删除错误,请将import lxml.etree as et xml = et.parse("trim.kml").getroot() print(xml.xpath("//kml:Document//kml:Placemark/kml:description", namespaces={"kml":xml.nsmap["kml"]})) 添加到第二行:

[<Element {http://www.opengis.net/kml/2.2}description at 0x7f612d0885f0>, <Element {http://www.opengis.net/kml/2.2}description at 0x7f612d088cb0>, <Element {http://www.opengis.net/kml/2.2}description at 0x7f612d088d40>, <Element {http://www.opengis.net/kml/2.2}description at 0x7f612d088d88>, <Element {http://www.opengis.net/kml/2.2}description at 0x7f612d088dd0>, <Element {http://www.opengis.net/kml/2.2}description at 0x7f612d088e18>]

然后使用lxml,以下工作:

document.placemark

这给了你:

from lxml import html
xml = html.parse("trim.kml")
print(xml.xpath("//placemark/description"))

你也可以使用 lxml.html ,它可以更好地处理破碎的xml,数据本身也是99%的html。

您可以从[<Element description at 0x7f1c757fad08>, <Element description at 0x7f1c757fad60>, <Element description at 0x7f1c757fadb8>, <Element description at 0x7f1c757fae10>, <Element description at 0x7f1c757fae68>, <Element description at 0x7f1c757faec0>] 内部获取一个:

{{1}}

这给了你:

{{1}}