2

I'm using python to create xml files, and I need to create an attribute like this

<element xml:id="something"/> some text 

I specifically used lcml because I need some text after the unique tag, I couldn't do it using DOM. If this is possible using DOM it would be great. how can I do this?

miradulo
  • 28,857
  • 6
  • 80
  • 93
Noro
  • 33
  • 4

2 Answers2

0

For adding attribute you should be doing:

import xml.etree.cElementTree as ET
ET.SubElement(root,'element').set('xml:id','something')

For adding text:

tree = ET.parse('country_data.xml')
root = tree.getroot()
for element in root.findall('element'):
    element.text = str("some text")
tree.write('output.xml')

The Etree documentation shows usage.

Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
0

You should use tail property:

etree_element.tail = ' some text'
apr
  • 370
  • 1
  • 5