xml

What is xml?

xml is a Python module for reading XML documents.

The XML documents are parsed to a tree-like structure of Python objects.

Exercises

Exercise 1

Store the following in an XML document:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <movieml name="XML example for teaching" author="Kristian Rother">
  3. <movielist name="movies">
  4. <movie id="1" name="Slumdog Millionaire (2008)">
  5. <rating value="8.0" votes="513804"></rating>
  6. </movie>
  7. <movie id="2" name="Planet of the Apes (1968)">
  8. <rating value="8.0" votes="119493"></rating>
  9. </movie>
  10. <movie id="3" name="12 Angry Men (1957)">
  11. <rating value="8.9" votes="323565"></rating>
  12. </movie>
  13. <movie id="4" name="Pulp Fiction (1994)">
  14. <rating value="8.9" votes="993081"></rating>
  15. </movie>
  16. <movie id="5" name="Schindler's List (1993)">
  17. <rating value="8.9" votes="652030"></rating>
  18. </movie>
  19. </movielist>
  20. <movielist name="serials">
  21. <movie id="6" name="Breaking Bad (2008)
  22. {To'hajiilee (#5.13)}">
  23. <rating value="9.7" votes="14799"></rating>
  24. </movie>
  25. <movie id="7" name="Game of Thrones (2011)
  26. {The Laws of Gods and Men (#4.6)}">
  27. <rating value="9.7" votes="13343"></rating>
  28. </movie>
  29. <movie id="8" name="Game of Thrones (2011)
  30. {The Lion and the Rose (#4.2)}">
  31. <rating value="9.7" votes="17564"></rating>
  32. </movie>
  33. <movie id="9" name="Game of Thrones (2011)
  34. {The Rains of Castamere (#3.9)}">
  35. <rating value="9.8" votes="27384"></rating>
  36. </movie>
  37. <movie id="10" name="Breaking Bad (2008)
  38. {Ozymandias (#5.14)}">
  39. <rating value="10.0" votes="55515"></rating>
  40. </movie>
  41. </movielist>
  42. </movieml>

Exercise 2

Run the following program:

  1. from xml.dom.minidom import parse
  2. document = parse('movies.xml')
  3. taglist = document.getElementsByTagName("movie")
  4. for tag in taglist:
  5. mid = tag.getAttribute('id')
  6. name = tag.getAttribute('name')
  7. print(mid, name)
  8. subtags = tag.getElementsByTagName('rating')
  9. print(subtags)

Exercise 3

Calculate the total number of votes.