获取Python中特定节点的所有子级 [英] Get all children of specific node in Python

查看:863
本文介绍了获取Python中特定节点的所有子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下example.xml结构:

I have the following example.xml structure:

<ParentOne>
   <SiblingOneA>This is Sibling One A</SiblingOneA>
   <SiblingTwoA>
      <ChildOneA>Value of child one A</ChildOneA>
      <ChildTwoA>Value of child two A</ChildTwoA>
   </SiblingTwoA>
</ParentOne>

<ParentTwo>
   <SiblingOneA>This is a different value for Sibling one A</SiblingOneA>
   <SiblingTwoA>
      <ChildOneA>This is a different value for Child one A</ChildOneA>
      <ChildTwoA>This is a different value for Child Two A</ChildTwoA>
   </SiblingTwoA>
</ParentTwo>

 <ParentThree>
   <SiblingOneA>A final value for Sibling one A</SiblingOneA>
   <SiblingTwoA>
      <ChildOneA>A final value for Child one A</ChildOneA>
      <ChildTwoA>A final value for Child one A</ChildTwoA>
   </SiblingTwoA>
</ParentThree>

我的主要要求是遍历每个节点,并且当所讨论的当前节点为"SiblingOneA"时,代码进行检查以查看直接相邻的同级节点是否为"SiblingTwoA".如果是这样,那么它应该检索所有子节点(元素本身以及元素中的值).

My main requirement is to loop through each one of the nodes and when the current node in question is "SiblingOneA", the code makes a check to see if the sibling node directly adjacent is "SiblingTwoA". If so, then it should retrieve all the children nodes (both the elements themselves, and the values within the elements).

到目前为止,这是我的代码:

So far, this is my code:

from lxml import etree
XMLDoc = etree.parse('example.xml')
rootXMLElement = XMLDoc.getroot()
tree = etree.parse('example.xml)
import os

for Node in XMLDoc.xpath('//*'):
   if os.path.basename(XMLDoc.getpath(Node)) == "SiblingOneA":
      if Node.getnext() is not None:
         if Node.getnext().tag == "SiblingTwoA":
            #RETRIEVE ALL THE CHILDREN ELEMENTS OF THAT SPECIFIC SiblingTwoA NODE AND THEIR VALUES

正如您从我上面的代码中推断的那样,我不知道该用什么代替注释来检索"SiblingTwoA"节点的所有子元素和值.另外,此代码不应返回整个树结构中SiblingTwoA节点的所有子元素,而仅返回所讨论的那个(即从Node.getnext()元素返回的那个).您还将注意到,许多元素都是相同的,但是它们的值是不同的.

As you may have deduced from my above code, I do not know what to put in place of the comment to retrieve all the children elements and values of the "SiblingTwoA" node. Also, this code should not return all the children elements of the SiblingTwoA nodes in the whole tree structure, but just of the one in question (i.e. the one returned from the Node.getnext() element). You will also have noticed that many of the elements are the same, but their values are different.

我已经能够使用Node.getnext().getchildren()检索有问题的元素的子级.但是,这将以列表的形式返回信息,例如:

I have been able to retrieve the children of the element in question using Node.getnext().getchildren(). However, this returns the information in the form of a list, such as:

[<Element ChildOneA at 0x101a95870>, <Element ChildTwoA at 0x101a958c0>]
[<Element ChildOneA at 0x101a95a50>, <Element ChildTwoA at 0x101a95aa0>]
[<Element ChildOneA at 0x101a95c30>, <Element ChildTwoA at 0x101a95c80>]

如何获取元素中的实际值?

How can I retrieve the actual values within the elements?

例如,对于第一次迭代,我想要的输出将是这样的:

My desired output, for the first iteration for example, would be something like:

ChildOneA =子A的值

ChildOneA = Value of child one A

ChildTwoA =子2 A的值

ChildTwoA = Value of child two A

推荐答案

我想生成一个可以使用的简单列表(['Value of child one A', 'Value of child two A', 'This is a different value for Child one A', 'This is a different value for Child Two A', 'A final value for Child one A', 'A final value for Child one A'])

I think to generate a simple list (['Value of child one A', 'Value of child two A', 'This is a different value for Child one A', 'This is a different value for Child Two A', 'A final value for Child one A', 'A final value for Child one A']) you can use

[child.xpath('string()') for sibling in doc.xpath('//SiblingTwoA[preceding-sibling::*[1][self::SiblingOneA]]') for child in sibling.xpath('*')]

生成可使用的嵌套列表([['Value of child one A', 'Value of child two A'], ['This is a different value for Child one A', 'This is a different value for Child Two A'], ['A final value for Child one A', 'A final value for Child one A']])

to generate a nested list ([['Value of child one A', 'Value of child two A'], ['This is a different value for Child one A', 'This is a different value for Child Two A'], ['A final value for Child one A', 'A final value for Child one A']]) you can use

[[child.xpath('string()') for child in sibling.xpath('*')] for sibling in doc.xpath('//SiblingTwoA[preceding-sibling::*[1][self::SiblingOneA]]')]

这篇关于获取Python中特定节点的所有子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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