Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

lxml

I have XML where the order of the child elements determines their z-order for display purposes. I use lxml.objectify to operate on the XML.

How do I change the position of a child element in objectify?

E.g. change:

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

To:

<canvas>
  <shape b>
  <shape a>
  <shape c>
</canvas>
like image 693
scanny Avatar asked Jan 30 '26 10:01

scanny


1 Answers

canvas.shape will be a list, so just modify the list:

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)
like image 69
Blender Avatar answered Feb 02 '26 01:02

Blender



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!