Homework 8

  1. (40 points, 20 points for each parser) This problem is a warm-up exercise with PHP's expat and simpleXML parser. Write separate scripts named expatformatter.php and simpleformatter.php that read an xml file that matches the cdcatalog.xml file specification and produces an html document with the following characteristics:

    1. The html page starts with a centered header that reads "CD Album Report".
    2. The html page then consists of a list of cds, separated by horizontal rules (hr tags).
    3. Each cd should have the following information listed

      1. CD title as a left-justified, h3 header.
      2. An unordered list with the following bullets:

        • artist
        • price: blue if the price is <= $9.00 and green otherwise.
        • year released

      A sample html page for the cdcatalog.xml file is shown here

  2. (30 points) Using the simpleXML parser, write a php script named avgpriceSimple.php that reads an xml file structured like the cdcatalog.xml and produces an ascii report similar to the one in class, where you have the headings Artist and Avg. Price and for each artist you print the artist's name in the format "Lastname, Firstname" and then the average price of that artist's cds. The output should be in alphabetical order by lastname. Here is a sample ascii report for the cdcatalog.xml file. If two artists have the same last name it does not matter which one appears first in the output. You will note that this problem is a bit more complicated than the one presented in class because 1) the cdcatalog is organized by cd rather than by artist, and 2) the artist's name is not divided into a first and lastname for you. To handle the first problem, I suggest you keep two associative arrays of artists, one for the number of cds encountered thus far and one for the total value of the cds. To handle the second problem I suggest that you use a Perl-style regular expression to separate the first and last names. You may assume that the first name is a single first name with no whitespace. If there is only one name, like Smiley or Dr.Hook, then you should assume that it is a firstname. You should suppress the comma if the artist has no last name (i.e., only the firstname should be printed).

    Once you have finished processing the cd's you can sort one of the associative arrays by lastname (use an artist's first name if the artist only has a first name), then iterate through the array, calculate the average price for each author, and print each author and the average price for that author's cds.

  3. (30 points) Repeat the previous problem except that you should now use the PHP DOM parser to solve the problem. Name your php script avgpriceDOM.php.