如何使用cElementTree检索父节点? [英] How to retrieve the parent node using cElementTree?

查看:102
本文介绍了如何使用cElementTree检索父节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于xml

<grandparent>
  <parent1>
     <child>data1</child>
  </parent1>
  <parent2>
     <child>data2</child>
  </parent2>
</grandparent>

我需要包含父级元组的列表,xml中每个父级的数据.

I need the list containing tuples of parent,data for each parent in xml.

有没有办法使用cElementTree?我能够为child,data做到这一点,但是不幸的是child在所有值上都是相同的,因此它用处不大.

Is there a way to do it USING cElementTree? I am able to do it for child,data, but unfortunately child is identical in all the values, hence it is of not much use.

推荐答案

parent_map = dict((c, p) for p in tree.getiterator() for c in p)
parent_map[el].remove(el)

这篇关于如何使用cElementTree检索父节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆