I am trying to check the MD5 hash of the text information in an XML element including all its children. This is required to interact with hipay services.
def CheckMD5(tree):
m = hashlib.md5()
body = tree.find('result')
m.update(ET.tostring(ET.ElementTree(body).getroot(), method="html"))
return tree.find('md5content').text == m.hexdigest()
Full code: https://github.com/fabiosantoscode/python-hipay/blob/master/hipay.py#L566
My problem is that calling ElementTree.tostring with method="xml" makes ElementTree collapse closing tags of empty elements, but using method="html" I lose capitalization. Example:
>>> ET.tostring(ET.fromstring('<rt><a></a><CapName>asd</CapName></rt>'), method='xml')
'<rt><a /><CapName>asd</CapName></rt>'
>>> ET.tostring(ET.fromstring('<rt><a></a><CapName>asd</CapName></rt>'), method='html')
'<rt><a></a><CapName>asd</capname></rt>'
>>>
Note:
I don't need to modify this XML document at all, I just need to extract the string data of the <result> element. If there are other libraries which allow me to do this, please indicate.