[solved] How to insert XML-element inside of text with Python? | SoloLearn: Learn to code for FREE!


[solved] How to insert XML-element inside of text with Python?

I have the code https://code.sololearn.com/cmL7fthzIr2Q/?ref=app where I want to convert <my_element>hello my world</my_element> into <my_element>hello <red>my</red> world</my_element> by means of the xml.etree.ElementTree module. Is this possible at all, and if it is, how would I have to proceed with this?

1/15/2021 8:30:36 PM

Jan Markus

13 Answers

New Answer


https://docs.python.org/3/library/xml.etree.elementtree.html im currently trying to read this, but it s python ;) im pretty sure the answer lies in the docs somewhere.


https://code.sololearn.com/c7xXubsHFcEk/?ref=app almost but no cigar i m also wondering how to prepend an element to another. or can we just do a textnode? i could append the first text node, then append the red node, then append the last text node. text node would be an element but without a tag sort of. ..... edit oh i just found something it s called "tail"




ChillPill I already read much on your suggested website but that's a lot of stuff and I did not have the time to go through completely yet.


Thank you very much, ChillPill . πŸ™‚πŸ‘ Result of this discussion is now part of https://code.sololearn.com/cTkYrCTYLzt4/?ref=app


ChillPill I have found a solution which seems to work at least, but I would appreciate to get a more elegant way to do this: https://code.sololearn.com/cEfGhO7EGu6a/?ref=app I stumbled upon this idea on https://stackoverflow.com/questions/53332815/how-to-create-a-sub-element-through-variable-in-python-3-6-5


ChillPill But I cannot remove the <red>...</red> element isolated as a child element. If I do this, the following 'world' is also removed, because it is the tail-element of the <red> element. 'world' is not a part of the text of <my_element> any more. see https://code.sololearn.com/cD0B1v4AU9iT/?ref=app Any solution?


i would suggest regular expressions. https://www.w3schools.com/python/python_regex.asp split the string exactly where the regex points to, then rejoin with a tag in between.


ChillPill My problem is not finding the word "my" in the example. I did this with re.search.... My problem is how to insert the XML correctly. I have extended the example which you can see here: https://code.sololearn.com/cb842dleRoKD/?ref=app The last output of print(prettify(root)) shows now <my_element>hello &lt;red&gt;my&lt;/red&gt; world</my_element> in the relevant line. That's not what I want. I want <my_element>hello <red>my</red> world</my_element> instead. Is that possible at all? If yes, then tell me how?


Adam McGregor that does not work with replace


oh i see. i think this a problem of "escaping" or "unescaping" html / xml characters. completely different kind of problem xD


Perhaps xsl would be the easiest and most suitable tool to deal with this, using regex in xsl:analyze-string? You could still call saxon xsl processor from within your python script... Or call etree.XSLT, for that matter.


Jan Markus have you considered the Python string replace method. https://www.w3schools.com/python/ref_string_replace.asp E.g from your code: root[0].text.replace("my", "<red>my</red>") Obviously, you can abstract this with a function and variables. Here's a list of other string methods you may find useful for working with strings. https://www.w3schools.com/python/python_ref_string.asp