Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python xml.etree.ElementTree append to subelement

I am trying to use xml.etree.ElementTree to parse a xml file, find a specific tag, append a child to that tag, append another child to the newly created tag and add text to the latter child.

My XML:

<root>
<a>
    <b>
      <c>text1</c>
    </b>
    <b>
      <c>text2</c>
   </b>
</a>
</root>    

Desired XML:

<root>
<a>
    <b>
      <c>text1</c>
    </b>
    <b>
      <c>text2</c>
   </b>
    <b>
      <c>text3</c>
   </b>
</a>
</root>

Current code:

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()


for x in root.iter():
    if (x.tag == 'a'):
        ET.SubElement(x, 'b')
        ET.SubElement(x, 'c')
        #add text

This seems to work except 'c' appends as a child of 'a' rather then 'b'

Like so:

<root>
<a>
    <b>
      <c>test1</c>
    </b>
    <b>
      <c>test2</c>
    </b>
  <b /><c/></a>
</root>

Also, how do I add text to the newly created element 'c'? I could iterate through until I find the a tag 'c' that has no text but there must be a better way.

like image 351
andrsnn Avatar asked Mar 31 '14 21:03

andrsnn


2 Answers

You need to specify b as a parent element for c.

Also, for getting the a tag you don't need a loop - just take the root (a).

import xml.etree.ElementTree as ET

tree = ET.parse('test.xml')
root = tree.getroot()

a = root.find('a')
b = ET.SubElement(a, 'b')
c = ET.SubElement(b, 'c')
c.text = 'text3'

print ET.tostring(root)

prints:

<root>
    <a>
        <b>
          <c>text1</c>
        </b>
        <b>
          <c>text2</c>
        </b>
        <b>
          <c>text3</c>
        </b>
    </a>
</root>
like image 150
alecxe Avatar answered Oct 26 '22 21:10

alecxe


I prefer to define my own function for adding text:

def SubElementWithText(parent, tag, text):
    attrib = {}
    element = parent.makeelement(tag, attrib)
    parent.append(element)
    element.text = text
    return element

Which then is very convenient to use as:

import xml.etree.ElementTree as ET

tree = ET.parse('test.xml')
root = tree.getroot()

a = root.find('a')
b = ET.SubElement(a, 'b')
c = SubElementWithText(b, 'c', 'text3')
like image 41
Yonatan Simson Avatar answered Oct 26 '22 19:10

Yonatan Simson