|
Laboratory Week 9 & 10 - XML Parsing and Processing with Perl
Learning Objectives:
- Learn how to use the XML::DOM module in perl to write simple XML processors.
Required Reading:
Lectures: Week 8 lecture 2
Access to Software:
All required software is available on gryphon.murdoch.edu.au. External students may download the required Perl module XML::DOM and install it on your own machine.
Instructions:
- The following instructions make use of Perl scripts available in the unit coordinator's directory (~hiew/examples/perl-xml/) on gryphon. Four of the scripts were described in the lectures.Use the command "cp" to copy the file over to your work directory on gryphon. Eg.
gryphon:~> mkdir ~/labwk9
gryphon:~> cp ~hiew/examples/perl-xml/* ~/labwk9
Read and understand ALL the perl processing scripts. Try the following commands in the directory where you have your files to assist in your understanding:
perl xml-dom1.pl
perl xml-dom2.pl course.xml
perl xml-dom3.pl course.xml
perl xml-dom4.pl course.xml
perl xml-dom5.pl course.xml staff.xml- Use the online Perl documentation for the XML::DOM module as reference, or the reference at http://cpan.uwinnipeg.ca/htdocs/libxml-enno/DOM.html (the same information, but slightly better formatting).
Exercises:
- Write a Perl script that counts the number of units in course.xml that has the unit title containing the word "Internet".
- Write a Perl script that only prints out the unit coordinator (surname and other names) of all units in course.xml that has the unit title constaining the word "Internet".
- Write a Perl script that reads a string from the user, and print all text in elements in course.xml where the element name is the same as string supplied by the user. Eg. if the user types in "surname", then print all text in the <surname> tags.
- Write a Perl script to transform all CDATA text in an XML file to lowercase and print the result to screen.
- Write a Perl script to delete all CDATA text lines in an XML file which have length more than 10 characters, and print the result to screen.
- Write a Perl script to allow a user to add data to the course.xml document. The script should prompt the user to type in a sequence of text. It should then insert the text into an element <info> under the root element <course>. Print out the result to screen.
- Write a Perl script to allow a user to add data to a particular unit in course.xml. The script should prompt the user for a course title, and also to type in a sequence of text. It should then insert the text into an element <info> under the <unit> element corresponding to the course title the user supplied. Print out the result to screen.
- (Tedious) The script xml-dom5.pl only adds unformated text from in staff.xml to an arbitrary element <info> in course.xml to merge information. Try writing a more elegant solution by having the elements in staff.xml inserted into appropriate elements in course.xml. [Note: Unfortunately with DOM level 1, you cannot straight-forward copy the nodes from staff.xml into course.xml. See http://www.w3.org/DOM/faq.html#ownerdoc. Therfore, you will have to recreate the nodes from staff.xml in course.xml. manually one node at a time.]
Assessable Tasks:
Exercises 1-6.
Internal students should demonstrate to your tutor by executing your scripts with your tutor present. This must be done in weeks 9, 10, or 11. No marks will be awarded if the work is demonstrated after that - your tutor have no discretionary power on this deadline.
External students should submit the scripts and sample runs during the deadline as indicated in the Study Schedule.
H.L. Hiew
Unit Coordinator
Document author: H.L.
Hiew, Unit Coordinator
Last Modified: Wednesday, 12-May-2004 17:39:55 MST
Disclaimer & Copyright
Notice © 2004 Murdoch University
This document is relevant for semester 1, 2004 only