簡體   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