2017-05-30 30 views
0

这里是文件如何返回此kml文件中的文件夹元素列表?

<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://www.opengis.net/kml/2.2"> 
    <Document> 
    <Folder> 
    <name>Points</name> 
    <Placemark> 
    <name>Port Saeed, Dubai</name> 
    <styleUrl>#icon-1899-0288D1-nodesc</styleUrl> 
    <Point> 
     <coordinates> 
     55.3295568,25.2513145,0 
     </coordinates> 
    </Point> 
    </Placemark> 
    <Placemark> 
    <name>Retail Location #1</name> 
    <description>Paris, France</description> 
    <styleUrl>#icon-1899-0288D1</styleUrl> 
    <Point> 
     <coordinates> 
     2.3620605,48.8867304,0 
     </coordinates> 
    </Point> 
    </Placemark> 
    <Placemark> 
    <name>Odessa Oblast</name> 
... 

我想提取“文件夹”元素

这里是我的代码的顶部。

tree = ET.parse(kml) 
root = tree.getroot() 

for element in root: 
    print element.findall('.//{http://www.opengis.net/kml/2.2/}Folder') 

此刻此打印[]。我相信它是命名空间的一个问题。我无法弄清楚如何创建该字符串?另外,也许它值得使用XPath呢?我想我会遇到与名称空间相同的问题

+0

不*文件夹*关闭标签? – Parfait

+0

是的,对不起,我没有粘贴整个文件,因为它会太长 – BigBoy1337

回答

1

考虑遍历文件夹的所有后代,因为此节点包含子元素和孙子元素。此外,解析中使用的命名空间前缀不应以正斜杠结尾。

import xml.etree.ElementTree as ET 

root = ET.fromstring('''<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://www.opengis.net/kml/2.2"> 
    <Document> 
    <Folder> 
     <name>Points</name> 
     <Placemark> 
     <name>Port Saeed, Dubai</name> 
     <styleUrl>#icon-1899-0288D1-nodesc</styleUrl> 
     <Point> 
      <coordinates> 
     55.3295568,25.2513145,0 
      </coordinates> 
     </Point> 
     </Placemark> 
     <Placemark> 
     <name>Retail Location #1</name> 
     <description>Paris, France</description> 
     <styleUrl>#icon-1899-0288D1</styleUrl> 
     <Point> 
      <coordinates> 
     2.3620605,48.8867304,0 
      </coordinates> 
     </Point> 
     </Placemark> 
    </Folder> 
    </Document> 
</kml>''') 

# FIND ALL FOLDERS 
for i in root.findall('.//{http://www.opengis.net/kml/2.2}Folder'): 
    # FIND ALL FOLDER'S DESCENDANTS 
    for inner in i.findall('.//*'): 
     data = inner.text.strip()  # STRIP LEAD/TRAIL WHITESPACE 
     if len(data) > 1:    # LEAVE OUT EMPTY ELEMENTS 
      print(data) 

# Points 
# Port Saeed, Dubai 
# icon-1899-0288D1-nodesc 
# 55.3295568,25.2513145,0 
# Retail Location #1 
# Paris, France 
# #icon-1899-0288D1 
# 2.3620605,48.8867304,0 

对于嵌套列表,节点文本追加到一个列表,其中每个内部列表对应于每个文件夹

data = [] 
for i in root.findall('.//{http://www.opengis.net/kml/2.2}Folder'): 
    inner = [] 
    for t in i.findall('.//*'): 
     txt = t.text.strip() 
     if len(txt) > 1: 
      inner.append(txt) 

    data.append(inner) 
相关问题