首页 > 解决方案 > AttributeError:“xml.etree.ElementTree.Element”对象没有属性“_children”

问题描述

我有以下代码:

def result_saml_decoded(result_saml):
"""Illustrate to result saml decoded value as response in a string.

:param result_saml:results saml decoded
:return:return principle_arn, resultsamldecoded, role_arns

"""
result_saml_decoded = base64.b64decode(result_saml)
root = ET.fromstring(result_saml_decoded)
principle_arns, role_arns = [], []
inner_saml_tag = [saml2 for saml2 in root._children if 'Assertion' in saml2.tag]
attribute_saml_tag = [saml_tag for saml_tag in inner_saml_tag[0]._children
                      if 'AttributeStatement' in saml_tag.tag]
for inner_saml_tag in attribute_saml_tag[0]._children:
    if 'uri' in inner_saml_tag.get('NameFormat'):
            for saml_data in inner_saml_tag._children:
                parts = saml_data.text.split(',')
                principle_arns.append(parts[0])
                role_arns.append(parts[1])

return principle_arns, role_arns

它在 python 2.7 中有效,但在 python 3.6 中失败:

回溯(最后一次调用):文件“/Users/kaulk/sandbox/oktapod1/oktapod/helpers.py”,第 135 行,在假设角色原则_arns,role_arns = result_saml_decoded(resultsaml) 文件“/Users/kaulk/sandbox/oktapod1/ oktapod/helpers.py",第 188 行,在 result_saml_decoded inner_saml_tag = [saml2 for saml2 in root._children if 'Assertion' in saml2.tag] AttributeError: 'xml.etree.ElementTree.Element' 对象没有属性 '_children'

对于应该与 py27 和 36 兼容的代码,我应该使用什么?

标签: pythonpython-3.xpython-2.7

解决方案


推荐阅读