使用Python和etree解析XML

时间:2015-04-20 08:08:02

标签: python xml xml-parsing elementtree

我想用密钥' highway'提取包含标签的所有方式元素。以及以下示例Open Street Map XML文件中的特定值:

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
 <bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>

 <node id="298884272" lat="54.0901447" lon="12.2516513" user="SvenHRO" uid="46882" visible="true" version="1" changeset="676636" timestamp="2008-09-21T21:37:45Z"/>
 <way id="26659127" user="Masch" uid="55988" visible="true" version="5" changeset="4142606" timestamp="2010-03-16T11:47:08Z">
  <nd ref="292403538"/>
  <nd ref="298884289"/>

  <nd ref="261728686"/>
  <tag k="highway" v="unclassified"/>
  <tag k="name" v="Pastower Straße"/>
 </way>
 <relation id="56688" user="kmvar" uid="56190" visible="true" version="28" changeset="6947637" timestamp="2011-01-12T14:23:49Z">
  <member type="node" ref="294942404" role=""/>
  ...
  <member type="node" ref="364933006" role=""/>
  <member type="way" ref="4579143" role=""/>
  ...
  <member type="node" ref="249673494" role=""/>
  <tag k="name" v="Küstenbus Linie 123"/>
  <tag k="network" v="VVW"/>
  <tag k="operator" v="Regionalverkehr Küste"/>
  <tag k="ref" v="123"/>
  <tag k="route" v="bus"/>
  <tag k="type" v="route"/>
 </relation>

</osm>

要做到这一点;我编写了以下使用Etree库的Python代码。它解析XML文档并使用findall函数(使用XPath语法)

import xml.etree.ElementTree as ET
supported_highways = ('motorway', 'trunk', 'primary', 'secondary', 'tertiary', 'unclassified', 'residential', 'highway_link', 'trunk_link', 'primary_link', 'secondary_link', 'tertiary_link')

class OSMParser:

    def __init__(self, inputData):
        self.root = ET.fromstring(inputData)


    def getRoads(self):
        ways = dict()
        for road in self.root.findall('./way/'):
            highway_tags = road.findall("./tag[@k='highway']")
            if not highway_tags:
                continue
            if all(highway.attrib['v'] not in supported_highways for highway in highway_tags):
                continue

然而,当我运行代码时,它找不到路径元素的标记(第二个findall产生一个空列表)。知道什么是错的吗?谢谢。

2 个答案:

答案 0 :(得分:1)

尝试使用XPath //way/代替./way/

答案 1 :(得分:0)

它的工作。

>>> root.findall("./way/tag[@k='highway']")
[<Element 'tag' at 0xb74568ac>]

我认为在您的输入内容标记way不是主要开始标记的子项,即根标记

或使用lxml.etree

>>> import lxml.etree as ET1
>>> root = ET1.fromstring(content)
>>> root.xpath("//way/tag[@k='highway']")
[<Element tag at 0xb745642c>]