久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用python獲取xml節點的所有父節點

get all parents of xml node using python(使用python獲取xml節點的所有父節點)
本文介紹了使用python獲取xml節點的所有父節點的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

對于這個xml

<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>

如何在 python 中使用 lxml etree 獲取節點的所有父節點.

How i can get all parents of a node using lxml etree in python.

預期輸出:輸入 orgid=126 ,它將返回所有喜歡的父母,

Expected output : Input orgid=126 , it will return all the parents like ,

{'A':124,'B':125,'C':126}

推薦答案

使用 lxml 和 XPath:

Using lxml and 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}

這篇關于使用python獲取xml節點的所有父節點的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 中文字幕不卡在线88 | 亚洲色片网站 | 国产亚洲精品成人av久久ww | 91美女在线观看 | 午夜网站视频 | 亚洲婷婷一区 | 羞羞视频在线观免费观看 | 天堂影院av | 99国产精品久久久久老师 | 亚洲国产一区二区三区四区 | 亚洲精品在线看 | 天天操天天射综合网 | 欧美国产精品 | 夜夜骑首页 | 超碰成人免费 | 国产精品国产 | 国产一区二区三区四区三区四 | 精品一级电影 | 日本免费视频在线观看 | 国产视频不卡一区 | 国产视频中文字幕 | 国产精品网址 | 麻豆久久久久久久 | 精品国产乱码久久久久久牛牛 | 日韩精品在线一区 | 久草中文在线 | 自拍偷拍亚洲欧美 | 欧美日韩在线观看一区 | 综合九九| 色在线视频网站 | 草草网| 天天躁日日躁狠狠躁白人 | 91久久久久 | 麻豆一区二区三区 | 五月花丁香婷婷 | 色在线视频网站 | 91综合网 | 男女性毛片 | 日韩精品在线观看一区二区三区 | 黄色精品 | 久久久蜜桃一区二区人 |