0

我有一个结构如下的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<TEI>
   A
   <placeName xml:id="ene.0" n="0" key="geonames 644285" ref="http://www.geonames.org/644285">Pralognan</placeName>
   suivre
   <placeName xml:id="ene.3" n="2" subtype="compound" key="osm 2272301" ref="http://www.openstreetmap.org/way/2272301">
      la route entre
      <placeName xml:id="ene.1" n="1" key="osm 178528565" ref="http://www.openstreetmap.org/node/178528565">
         l'hôtel  de la
         <placeName n="0">Vanoise</placeName>
      </placeName>
      et celui du
      <placeName xml:id="ene.2" n="0" key="osm 3379120" ref="http://www.openstreetmap.org/way/3379120">Petit Mont Blanc</placeName>
   </placeName>
</TEI>

以及解析它的python代码:

import xml.etree.cElementTree as ET
parse_file    = open("file.xml","r")
tree_parse_file = ET.parse(parse_file)
root_parse_file = tree_parse_file.getroot()

for child in root_parse_file: # Child pointing on all sub child of root
    if "ref" in child.attrib.keys():
        #some code...
        for subChild in child: # To point on all of subChild of Child elements, this is line 59 of my code
        print(subChild.attrib['ref'])
        #some code... 

当我想迭代这个元素时

<placeName xml:id="ene.3" ...>

要获取所有嵌套元素并解析它们的属性,我在这一行收到以下错误:print(subChild.attrib['ref']) 错误:

Traceback (most recent call last):
  File "./generate_long_lat2.py", line 59, in <module>
    print(subChild.attrib['ref'])
KeyError: 'ref'

并且属性ref存在于元素的子子元素中

<placeName xml:id="ene.1" ...>

我的问题是如何遍历根元素的所有嵌套子子元素?

4

1 回答 1

1

要遍历特定标签的属性,您可以使用以下代码(包含 id 的标签 placeName):

from lxml import etree

tree = etree.parse("file.xml")

for attributes in tree.xpath("//placeName[(@xml:id)]"):
    for name, value in attributes.items():
        print(f'{name} = {value}')

输出:

{http://www.w3.org/XML/1998/namespace}id = ene.0
n = 0
key = geonames 644285
ref = http://www.geonames.org/644285
{http://www.w3.org/XML/1998/namespace}id = ene.3
n = 2
subtype = compound
key = osm 2272301
ref = http://www.openstreetmap.org/way/2272301
{http://www.w3.org/XML/1998/namespace}id = ene.1
n = 1
key = osm 178528565
ref = http://www.openstreetmap.org/node/178528565
{http://www.w3.org/XML/1998/namespace}id = ene.2
n = 0
key = osm 3379120
ref = http://www.openstreetmap.org/way/3379120

此处的文档-> https://lxml.de/tutorial.html#elements-carry-attributes-as-a-dict

于 2019-03-30T14:05:21.163 回答