简体   繁体   English

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

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

I have XML where the order of the child elements determines their z-order for display purposes. 我有XML,其中子元素的顺序决定了其z顺序以用于显示。 I use lxml.objectify to operate on the XML. 我使用lxml.objectify对XML进行操作。

How do I change the position of a child element in objectify? 如何在objectify中更改子元素的位置?

Eg change: 例如更改:

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

To: 至:

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

canvas.shape will be a list, so just modify the list: 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