1 year ago
#384864
Sem Lion
School assignment XML Document
all I need mini help with this I need to create a list of movies in XML document and have a build that. I'm stuck here. Now I need to print in string tnx all in advance.
import xml.etree.ElementTree as ET
def view(elem,level=0):
i = "\n" + level*" "
j = "\n" + (level-2)*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = I
for subelem in elem:
view(subelem, level+2)
if not elem.tail or not elem.tail.strip():
elem.tail = I
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = j
return elem
root = ET.Element("Movies")
movie = ET.SubElement(root,"Movie")
info = ET.SubElement(movie,"Info")
Name_of_the_movie = ET.SubElement(info,"Name of the film")
Name_of_the_movie.text = "The Shawshank Redemption"
Year_of_film = ET.SubElement(info,"Year of film")
Year_of_film.text = "1994"
Genre_of_film = ET.SubElement(info,"Genre of film")
Genre_of_film.text = "Drama"
movie = ET.SubElement(root,"Movie")
info = ET.SubElement(movie,"Info")
Name_of_the_movie = ET.SubElement(info,"Name of the film")
Name_of_the_movie.text = "The Godfather"
Year_of_film = ET.SubElement(info,"Year of film")
Year_of_film.text = "1972"
Genre_of_film = ET.SubElement(info,"Genre of film")
Genre_of_film.text = "Crime,Drama"
tree = ET.ElementTree(view(root))
tree.write("Movies_Test1.xml",xml_declaration=True,encoding="utf-8")
python
xml
xml-parsing
0 Answers
Your Answer