Python Forum
xml element inside text node
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xml element inside text node
#8
While looking for a solution… I think there might be something around .tail. I don't really understand the .tail principle and an answer could be there.

No?

OK. This works.

import xml.etree.ElementTree as ET
import re

action = ET.Element('act')
action.text = 'A guy enters the room and **Slam!** shuts the door.'

look = re.search('\*\*.+\*\*',action.text)

if look != None:
	t1 = action.text[0:look.span()[0]]
	t2 = action.text[look.span()[0]+2:look.span()[1]-2]
	t3 = action.text[look.span()[1]:]
	action.text = t1
	bold = ET.SubElement(action,'bold')
	bold.text = t2
	bold.tail = t3

print(ET.tostring(action))
If someone finds something to do better…
Reply


Messages In This Thread
xml element inside text node - by SgtPembry - Oct-21-2018, 01:37 PM
RE: xml element inside text node - by Larz60+ - Oct-21-2018, 02:59 PM
RE: xml element inside text node - by SgtPembry - Oct-21-2018, 03:11 PM
RE: xml element inside text node - by Larz60+ - Oct-21-2018, 03:45 PM
RE: xml element inside text node - by SgtPembry - Oct-21-2018, 03:56 PM
RE: xml element inside text node - by snippsat - Oct-21-2018, 05:13 PM
RE: xml element inside text node - by SgtPembry - Oct-21-2018, 07:47 PM
RE: xml element inside text node - by SgtPembry - Oct-21-2018, 10:17 PM
RE: xml element inside text node - by snippsat - Oct-21-2018, 11:05 PM
RE: xml element inside text node - by SgtPembry - Oct-21-2018, 11:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to locate element no such element gahhon 6 4,612 Feb-18-2019, 02:09 PM
Last Post: gahhon
  how to enter text in text box inside a program? Astronometria 0 2,498 Mar-10-2018, 11:54 PM
Last Post: Astronometria
  Change single element in 2D list changes every 1D element AceScottie 9 12,203 Nov-13-2017, 07:05 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020