Working code implemented in Python and appropriate comments provided for better understanding:
Here I am attaching code for these files:
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.
Problem 4 (DOM operations - 30 points): Consider the Python code snippet bookstore.py for DOM operation,...