Python Forum
[XML] How to add new element to block? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [XML] How to add new element to block? (/thread-29339.html)



[XML] How to add new element to block? - Winfried - Aug-28-2020

Hello,

Using BeautifulSoup, I can't figure out how to add a new element to the current block:

from bs4 import BeautifulSoup as Soup

with open('track.kml') as data:
    kml_soup = Soup(data, 'lxml-xml') # Parse as XML

placemarks = kml_soup.find_all('Placemark')
for pm in placemarks:
	if pm.find('LineString'):
		print("LS found")
		#How to insert new element before LineString?

		#TypeError: 'NoneType' object is not callable
		tag = pm.new_tag("Blah")
		tag.string = "<Another>blah</Another>"
		pm.string.insert_after(tag)
		print(pm)
Thank you.