scanny
scanny

Reputation: 28863

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. 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>

Upvotes: 3

Views: 975

Answers (1)

Blender
Blender

Reputation: 298056

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)

Upvotes: 3

Related Questions