本文介绍了Python elementtree find函数将Signature读取为空(无)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用ElementTree从Python中的已签名XML读取Signature,SignatureValue和SignedInfo,但其读数为None。
其他xml属性都可以阅读。如何阅读Signature,SignatureValue和SignedInfo?
I'm trying to read Signature, SignatureValue and SignedInfo from signed XML in Python with ElementTree but it reads as None.Other xml attributes are read fine. How can I read Signature, SignatureValue and SignedInfo?
这是我的代码段:
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:
Here's my test 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>
推荐答案
这是因为在Signature元素上有名称空间,
您可以做的就是找到具有命名空间的元素
Its because you have namespace on the Signature element,what you can do is find the element with the namespace
print response_xml.find('{http://www.w3.org/2000/09/xmldsig#}Signature')
然后您将需要签名元素并从中找到所有子元素
then you will have to take the signature element and find all sub elements from it
类似这样的东西:
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')
这篇关于Python elementtree find函数将Signature读取为空(无)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!