1

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.

1 Answer 1

1

LH.tostring does not collapse closing tags:

In [34]: import lxml.etree as ET

In [35]: import lxml.html as LH

In [36]: LH.tostring(ET.fromstring('<rt><a></a><CapName>asd</CapName></rt>'))
Out[36]: '<rt><a></a><CapName>asd</CapName></rt>'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.