Python Forum

Full Version: [XML] How to add new element to block?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.