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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

well, I still need to write some text after the tag: <element xml:id="something"/> texttexttext
0

You should use tail property:

etree_element.tail = ' some text'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.