Python elementtree find函数将签名读取为空(无)
我试图用ElementTree从Python中的签名XML中读取Signature、SignatureValue和SignedInfo,但它的读取结果是None。 其他xml属性可以很好地读取。如何阅读签名、SignatureValue和SignedInfo 以下是我的代码片段:Python elementtree find函数将签名读取为空(无),python,xml,elementtree,xml-signature,Python,Xml,Elementtree,Xml Signature,我试图用ElementTree从Python中的签名XML中读取Signature、SignatureValue和SignedInfo,但它的读取结果是None。 其他xml属性可以很好地读取。如何阅读签名、SignatureValue和SignedInfo 以下是我的代码片段: xml_file = open(settings.STATIC_ROOT + '/file/test.xml', 'rt').read() response_xml = xml_et.fromstring(xml_fi
xml_file = open(settings.STATIC_ROOT + '/file/test.xml', 'rt').read()
response_xml = xml_et.fromstring(xml_file.encode('utf-8'))
print response_xml.find('Signature') # prints None
print response_xml.find('SignatureValue') # prints None
print response_xml.find('SignedInfo') # prints None
print response_xml.find('OrderID').text # works fine
以下是我的测试XML:
<?xml version="1.0" encoding="UTF-8"?>
<Message date="08/09/2016 06:47:20">
<Version>1.0</Version>
<OrderID>ABCD:123456</OrderID>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>blabla=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>blabla==</SignatureValue>
<KeyInfo>
<KeyName>Public key of certificate</KeyName>
<KeyValue>
<RSAKeyValue>
<Modulus>blabla==</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
</KeyValue>
<X509Data>
<X509Certificate>blabla</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</Message>
1
ABCD:123456
布拉布拉=
布拉布拉==
证书公钥
布拉布拉==
AQAB
布拉布拉
这是因为签名元素上有名称空间,
您可以做的是找到具有名称空间的元素
print response_xml.find('{http://www.w3.org/2000/09/xmldsig#}Signature')
然后,您必须获取签名元素并从中查找所有子元素
大概是这样的:
namespace = "{http://www.w3.org/2000/09/xmldsig#}"
signature_elem = response_xml.find(namespace +'Signature')
print signature_elem
print signature_elem.find(namespace+'SignatureValue')
print signature_elem.find(namespace+'SignedInfo')