Lab Sheet 7
The goal of this lab will be to explore various options for serializing/deserializing the
Address Book as a way to implement its import/export functionality..
You may have already explored this opt
ion in previous labs, but in any case, this is the
time to look into using the Java Serialization mechanism to export and import the
Address Book… Make the AddressBook class (and anything else that need be!)
implement Serializable, and reimplement the impo
rt and export methods to make use of
it. The JUnit tests that you wrote for lab 6 to test import/export will probably not be
affected by the changes if the method signatures of import/export are the same (that is the
beauty of regression testing with JUnit
!) and should still be provided to evaluate the
correctness of the implementation.
Serialization with XML
Now let’s use XML to serialize our Address Book... To do so, we first need to find an
In Eclipse you might actually have a cho
ice between two SAX Parsers! If the Java SDK
version is 1.4 or up, it comes with the JAXP library, which can simply be found by
importing the org.xml.sax.* packages. If not, you can also use the Xerces parser, which is
bundled with Eclipse. In the latter c
ase, you need to add it to your “build path”, i.e. tell
the Java compiler where to find the code for that framework. Just like you did for JUnit,
you’d need to:
Open the project that contains your Address Book application
click on the project name an
d select “Properties”
Select the “Libraries” tab.
Click on “Add External JARs” (JARs are like Zip files for Java classes)
Look for a file called “xmlParserAPIs.jar”, which should be in a folder called
“org.apache.xerces_4.0.7” (or some more recent version
), itself contained in a folder
called “plugins”, itself contained in the “eclipse” folder. The location might be
different on your installation of Eclipse, but I am sure you will find it!
Select the JAR file, click open and you should be ready to go!
next step is to actually come up with an XML format for the Address Book. The
simplest idea is to have an element called AddressBook, have BuddyInfo and other
instance variables of AddressBook nested as subelements of AddressBook, and finally
variables of BuddyInfo nested as subelements of BuddyInfo…
Since we do not want to rewrite the import/export methods again as we are happy with
the Java Serialization solution from Part 1, let’s define the following methods:
public String toXML(): will s
hadow the toString() methods already defined for
AddressBook and BuddyInfo except that the output will be in the XML format
that you have defined.
exportToXmlFile() and importFromXmlFile() (complete signature is left to you to
define!) provided in AddressB
you also need to handle some of the SAX events generated by the parser. One
place is to do so is the AddressBook class, which would then have to extend
Of course, the new import/export functionality should be demonstrated in new JUnit
Deliverables: the source code of the following classes: AddressBookTest,
BuddyInfoTest, and the corresponding AddressBook and BuddyInfo. Make sure that your
code is properly commented!
Submit your files on the course WebCT page.
March 2nd, 5:30pm.