如何获取lxml.etree的父标签属性,例如“ KEY”,“ NAME”,Python 3.6

时间:2019-05-09 13:06:50

标签: python python-3.x pandas dataframe lxml

我想找到XML标签的父级,如何获取标签属性。

import requests
from lxml import etree
from io import StringIO, BytesIO

Request_URL = 'http://dataportal.ins.tn/WebApi/GetDimensionElements'
Method_Post_Body = "<QueryMessage lcid='1033'> <DataWhere> <DimensionId>OBJ5258839</DimensionId> </DataWhere> </QueryMessage>"
Post_Response = requests.post(Request_URL, data=Method_Post_Body, headers={'Content-type': 'text/xml'})

XRoot = etree.fromstring(Post_Response.content)
for Tag_1 in XRoot[1]:
    for element in Tag_1.iter():
    if element.getparent() is not None:
        print("parent-path:", element.getroottree().getpath(element.getparent())) 

2 个答案:

答案 0 :(得分:1)

此代码应为您提供带有键/值对的元组列表:

for i in XRoot.xpath("//*['Key']"):
    print(i.items())

示例输出:

[('Id', 'FULLNAME'), ('Key', '27880399'), ('Name', 'Nom complet')]

然后可以将它们添加到数据框或其他任何内容中。

答案 1 :(得分:0)

下面的代码对我有帮助:

XML_List = []
XML_Structure_All = pd.DataFrame()
for Tag_1 in XRoot[1]:
    for Child in Tag_1.iter():
    if len(Child.getparent().attrib) > 0:
        if 'CODE' in Child.getparent().attrib.keys():
        Parent = Child.getparent().attrib['CODE']
        elif 'C_CODE' in Child.getparent().attrib.keys():
        Parent = Child.getparent().attrib['C_CODE']
        elif 'KEY' in Child.getparent().attrib.keys():
        Parent = Child.getparent().attrib['KEY']
    else:
        Parent = ''

    if 'CODE' in Child.attrib.keys(): Col = 'CODE'
    elif 'C_CODE' in Child.attrib.keys(): Col = 'C_CODE'
    elif 'KEY' in Child.attrib.keys(): Col = 'KEY'

    XML_Dict = {'CODE': Child.attrib[Col], 'Parent': Parent}
    XML_List.append(XML_Dict)
XML_Dimension_Parent = pd.DataFrame(XML_List)