Question

Problem 4 (DOM operations - 30 points): Consider the Python code snippet bookstore.py for DOM operation, some basic methods a(a) Write a Python function get_text(elm) to extract all the text of a dom tree pointed by elm. (b) Write a Python function t

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Working code implemented in Python and appropriate comments provided for better understanding:

Here I am attaching code for these files:

  • bookstore.py
  • bookstore.xml

Source code for bookstore.py:

import re
from xml.dom.minidom import parse, parseString

def process_dom_tree(dm):
   lst = []
   elms = dm.getElementsByTagName('book')
   for elm in elms:
       l = get_text(elm)
       lst.append(l)
   return lst

def get_text(elm):
   lst = []
   if elm.nodeType in (3,4):
       if not elm.data.isspace():
           lst.append(elm.data)
   else:
       for child in elm.childNodes:
           lst = lst + get_text(child)
   return lst
  
# Retrieve all the text strings for the books published before 1990.
def get_old_books():
   return list(filter(lambda x: int(x[2]) < 1990, process_dom_tree(dom)))

def main():
   lst = []
   global dom
   dom = parse('bookstore.xml')
   lst = process_dom_tree(dom)
   print(lst)
   print("\nBooks published before 1990:")
   print(get_old_books())
   return lst

if __name__ == "__main__":
   main()

Code Screenshots:

Source code for bookstore.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
   <book category="computer">
       <title lang="en">Emacs User Manual</title>
       <author>Richard Stallman</author>
       <year>1980</year>
       <price>12.00</price>
   </book>
   <book category="scifi" cover="paperback">
       <title lang="en">Timeline</title>
       <author>Michael Chricton</author>
       <year>1999</year>
       <price>15.00</price>
   </book>
   <book category="comedy" cover="hardcopy">
       <title lang="en">Catch 22</title>
       <author>Joseph Heller</author>
       <year>1961</year>
       <price>20.00</price>
   </book>
   <book category="mystery" cover="paperback">
       <title lang="en">Lost Symbol</title>
       <author>Dan Brown</author>
       <year>2009</year>
       <price>15.00</price>
   </book>
   <book category="comedy" cover="hardcopy">
       <title lang="en">The Hitchhikers Guide to The Galaxy</title>
       <author>Doug Adams</author>
       <year>1978</year>
       <price>10.00</price>
   </book>
</bookstore>

Sample Output Screenshots:

Hope it helps, if you like the answer give it a thumbs up. Thank you.

Add a comment
Know the answer?
Add Answer to:
Problem 4 (DOM operations - 30 points): Consider the Python code snippet bookstore.py for DOM operation,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT