Big Java Lab 26

Parsing XML Documents

1.

Consider the following XML file that describes a pizza:
<pizza>
   <name>BBQ Chicken</name>
   <description>
       <crust>thin</crust>
       <sauce>barbecue</sauce>
       <cheese>Gouda</cheese>
       <cheese>mozzarella</cheese>
       <toppings>
           <meat>chicken</meat>
           <vegetable>red onion</vegetable>
           <vegetable>cilantro</vegetable>
       </toppings>
   </description>
</pizza>

Make a similar XML file that describes your favorite pizza.


2.

Write a program that can read XML files for describing a pizza, in the format of the preceding exercise.

Read the information in the file and create an instance of a class Pizza and then display it on screen, including the total pizza price. You can use the Pizza class and helper classes that you created in lab 17.

What is the code of your parser class?


3.

What is the code of your tester class?



4.

Creating XML Documents

Write a program to create a pizza restaurant menu stored as an XML file. Read in several XML files describing pizzas, in the same format as before. Make a Menu object that contains an ArrayList<Item>. Each item contains a pizza and a price.

Your program should then produce an XML document with a <menu> root element describing the menu:

<menu>
   <item>
      <pizza>
          . . .
      </pizza>
      <price>...</price>
   </item>
   <item>
      <pizza>
          . . .
      </pizza>
   <price>...</price>
   </item>
</menu>

What is the code of your builder class?


5.

What is the code of your tester class?

Document Type Definitions (DTDs)

6.

Design a document type definition (DTD) that describes a pizza.

Include a name and description, including crust type, toppings, and sauce.


7.

Write a program that can read and validate XML files for describing a pizza.

Modify your existing program to read an XML file containing a pizza description, using a DTD for validation.

How does your parser class change?


8.

What happens if you use the following as your pizza.xml file?


<?xml version="1.0"?>
<!DOCTYPE pizza SYSTEM "pizza.dtd">
<pizza>
   <name>BBQ Chicken</name>
   <description>
      <crust>thin</crust>
      <size>medium</size>
      <sauce>barbecue</sauce>
      <cheese>gouda</cheese>
      <cheese>mozzarella</cheese>
      <toppings>
         <topping>chicken</topping>
         <topping>red onion</topping>
         <topping>cilantro</topping>
      </toppings>
   </description>
   <price currency="USD">16</price>
</pizza>