且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用python获取xml节点的所有父级

更新时间:2023-02-05 20:14:05

使用lxml和XPath:

>>> s = '''
... <Departments orgID="123" name="xmllist">
...     <Department>
...         <orgID>124</orgID>
...         <name>A</name>
...         <type>type a</type>
...         <status>Active</status>
...             <Department>
...                 <orgID>125</orgID>
...                 <name>B</name>
...                 <type>type b</type>
...                 <status>Active</status>
...                 <Department>
...                     <orgID>126</orgID>
...                     <name>C</name>
...                     <type>type c</type>
...                     <status>Active</status>
...                 </Department>
...             </Department>
...     </Department>
...     <Department>
...         <orgID>109449</orgID>
...         <name>D</name>
...         <type>type d</type>
...         <status>Active</status>
...     </Department>
... </Departments>
... '''


使用ancestor-or-self轴,您可以找到节点本身,父级,祖父母,...


Using ancestor-or-self axis, you can find the node itself, parent, grandparent, ...

>>> import lxml.etree as ET
>>> root = ET.fromstring(s)
>>> for target in root.xpath('.//Department/orgID[text()="126"]'):
...     d = {
...         dept.find('name').text: int(dept.find('orgID').text)
...         for dept in target.xpath('ancestor-or-self::Department')
...     }
...     print(d)
...
{'A': 124, 'C': 126, 'B': 125}