繁体   English   中英

如何更改lxml objectify中子元素的顺序?

[英]How do I change the order of a child element in lxml objectify?

我有XML,其中子元素的顺序决定了其z顺序以用于显示。 我使用lxml.objectify对XML进行操作。

如何在objectify中更改子元素的位置?

例如更改:

<canvas>
  <shape a>
  <shape b>
  <shape c>
</canvas>

至:

<canvas>
  <shape b>
  <shape a>
  <shape c>
</canvas>

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM