Skip to content

Instantly share code, notes, and snippets.

@VioletVivirand
Created February 1, 2019 08:42
Show Gist options
  • Save VioletVivirand/81e9dfbfc27ebd56cf54de186c1f5c6c to your computer and use it in GitHub Desktop.
Save VioletVivirand/81e9dfbfc27ebd56cf54de186c1f5c6c to your computer and use it in GitHub Desktop.
BeautifulSoup with XML
from bs4 import BeautifulSoup
# Read XML with XML parser
with open('template.xml') as fp:
soup = BeautifulSoup(fp, 'xml')
# Modify value of a tag
soup.size.width.string = 'Something New'
# Equal to soup.annotation.size.width
# Add a tag
obj_tag = soup.new_tag('object')
soup.annotation.append(obj_tag)
# Print with prettified text
print(soup.prettify())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment