I have been using lxml to generate XML to interface with Authorize.net's CIM API and I noticed something. Element does not behave as expected, which is supposed to be as a list. The key difference is with references to the same Element.
1 2 3 4 5 6 7 8 | #!/usr/bin/env python from lxml import etree root = etree.Element('root') child = etree.Element('child') root.append(child) root.append(child) print etree.tostring(root, pretty_print=True) |
If you run this, you will get the following output:
<root> <child/> </root>
One child when we are expecting two. This is a bug. The workaround is to use deepcopy.
1 2 3 4 5 6 7 8 9 | #!/usr/bin/env python from copy import deepcopy from lxml import etree root = etree.Element('root') child = etree.Element('child') root.append(child) root.append(deepcopy(child)) print etree.tostring(root, pretty_print=True) |
This results with the expected output:
<root> <child/> <child/> </root>

At least some bloggers can write. My thanks for this piece..