我有XML,其中子元素的顺序决定了它们的z顺序以便显示。我使用lxml.objectify来操作XML。
如何在客观化中改变子元素的位置?
E.g。改变:
<canvas>
<shape a>
<shape b>
<shape c>
</canvas>
要:
<canvas>
<shape b>
<shape a>
<shape c>
</canvas>
答案 0 :(得分:2)
canvas.shape
将是一个列表,因此只需修改列表:
from lxml import objectify, etree
canvas = objectify.fromstring('''
<canvas>
<shape name="a" />
<shape name="b" />
<shape name="c" />
</canvas>
''')
canvas.shape = [canvas.shape[1], canvas.shape[0], canvas.shape[2]]
print etree.tostring(canvas, pretty_print=True)