Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lxml: insert tag at a given position

Tags:

python

lxml

I have an xml file, similar to this:

<tag attrib1='I'>
  <subtag1 subattrib1='1'>
    <subtext>text1</subtext>
  </subtag1>
  <subtag3 subattrib3='3'>
    <subtext>text3</subtext>
  </subtag3>
</tag>

I would like to insert a new subElement, so the result would be something like this

<tag attrib1='I'>
  <subtag1 subattrib1='1'>
    <subtext>text1</subtext>
  </subtag1>
  <subtag2 subattrib2='2'>
    <subtext>text2</subtext>
  </subtag2>
  <subtag3 subattrib3='3'>
    <subtext>text3</subtext>
  </subtag3>
</tag>

I can append my xml file, but then the new elements will be inserted at the end. How can I force python lxml to put it into a given position?

Thanks for your help!

like image 498
L D Avatar asked Apr 09 '13 18:04

L D


People also ask

What is lxml objectify?

The lxml. objectify, element trees provide an API that models the behaviour of normal Python object trees as closely as possible.

Is XML and lxml are same?

lxml is a reference to the XML toolkit in a pythonic way which is internally being bound with two specific libraries of C language, libxml2, and libxslt. lxml is unique in a way that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API.


1 Answers

You can use the addnext() method:

from lxml import etree

XML= """
<tag attrib1='I'>
  <subtag1 subattrib1='1'>
    <subtext>text1</subtext>
  </subtag1>
  <subtag3 subattrib3='3'>
    <subtext>text3</subtext>
  </subtag3>
</tag>"""

parser = etree.XMLParser(remove_blank_text=True)
tag = etree.fromstring(XML, parser)

subtag1 = tag.find("subtag1")
subtag2 = etree.Element("subtag2", subattrib2="2")
subtext = etree.SubElement(subtag2, "subtext")
subtext.text = "text2"
subtag1.addnext(subtag2)   # Add subtag2 as a following sibling of subtag1

print etree.tostring(tag, pretty_print=True)

Output:

<tag attrib1="I">
  <subtag1 subattrib1="1">
    <subtext>text1</subtext>
  </subtag1>
  <subtag2 subattrib2="2">
    <subtext>text2</subtext>
  </subtag2>
  <subtag3 subattrib3="3">
    <subtext>text3</subtext>
  </subtag3>
</tag>

Alternative: use insert() on the root element:

subtag2 = etree.Element("subtag2", subattrib2="2")
subtext = etree.SubElement(subtag2, "subtext")
subtext.text = "text2"
tag.insert(1, subtag2)   # Add subtag2 as the second child (index 1) of the root element
like image 94
mzjn Avatar answered Oct 21 '22 06:10

mzjn