XML and Java Objects, Part 1

by Richard G. Baldwin
baldwin@austin.cc.tx.us
Baldwin's Home Page

Dateline: 06/27/99

prolog

In a series of earlier articles entitled "What is SAX..." I introduced you to SAX and showed you some techniques for using SAX to write XML/Java applications.

This new article is the first in a new series involving SAX and Java. In this series, I will show you how to use SAX to convert an XML document into a set of Java objects, and how to convert a set of Java objects into an XML document.

In a future series of articles, I will show you how to do similar things using Java and the Document Object Model (DOM).

overview

This series of articles will provide a sample program that parses an XML file and extracts the data contained in the file. The data extracted from the XML file will be used to create a set of Java objects.

Those objects would be suitable for a variety of purposes, such as editing. However, since this series of articles is primarily concerned with the conversion back and forth between XML files and Java objects, I won't manipulate the data in the objects at this time (I will probably show you how to do that in the future).

After creating a set of objects containing the data from the XML file, the data contained in the objects will be used to produce another XML file. Had the objects been edited in the interim, the result would be a new XML file that is an edited version of the original. (I actually use a scheme similar to this for maintaining my HTML pages at About.com. In that case, I have an additional program that converts edited XML data to HTML files.)

As it turns out, for a specific XML file format, it is easy to write a Java application that provides a GUI editing capability. What do I mean by a GUI editing capability? I mean an editor that presents the XML data in a graphical user interface containing text fields, check boxes, buttons, etc., to facilitate editing the XML file without the requirement to know anything about XML syntax.

(Writing a general-purpose GUI editor that is not tied to a specific XML format would be somewhat more difficult.)

The sample program used in this series of articles is designed for a very specific XML file format, as described in the following section.

the XML file, an examination

The XML file used with this sample program represents the rudimentary aspects of a set of examination questions. (Since I am a college professor, you might know that I would be interested in examinations. I'm particularly interested in examinations that know how to score themselves.)

The first listing below shows a schematic of the element structure of the XML file along with the attributes of the elements. The second and third listings show the actual XML file that was used to test the program, including the content of each of the elements.

In some future series of articles, I plan to show you how to turn this into a self-scoring examination.

<?xml version="1.0"?>
<exam>
  <problem problemNumber="1">
    <question>...</question>
    <answer type="multiple" 
            numberChoices="4" 
            valid="0,1,3">
      <item>...</item>
      <item>...</item>
      <item>...</item>
      <item>...</item>
    </answer>
    <explanation>...</explanation>
  </problem>

  <problem problemNumber="2">
    ...
  </problem>
</exam>

XML file structure

The XML file uses the following types of elements as highlighted above:

the exam element

The XML document contains a single exam element, and the exam element contains one or more problem elements.

the problem elements

The problem elements have an attribute that specifies the problem number associated with each problem element.

Each problem element also contains

the question element

The question element is simply what it sounds like. It asks the question for a particular problem.

the answer element

Each answer element contains one or more item elements and some attribute values. The attribute values explain how different parts of the answer element should be used to construct the correct answer to the question.

the explanation element

The explanation element is some text that explains the answer.

example problem element

For example, the attributes of the answer element of the case shown above (and repeated below) represent a multiple-choice question having four available choices.

    <answer type="multiple" 
            numberChoices="4" 
            valid="0,1,3">

In an online testing situation, the student would select one or more of the item elements as her answer to the question. In this particular case, the student would need to select items 0, 1, and 3 in order to provide a correct answer to the question.

credit

The idea of using XML to represent exam problems is such a good idea that I wish I could claim it for my own. Unfortunately, I can't. The idea came from an article published by Claude Duguay in the April, 1999 issue of Java Pro magazine. He uses this concept to illustrate the use of XML and Java Servlets to provide an online testing capability. As of 6/27/99, the article is available online at www.devx.com.

the XML file

A two-part listing of the actual XML file used to test the sample program is shown below. Note that this file contains one exam element with two problem elements. The first problem, shown in the first listing, has a multiple-choice question requiring the selection of three of four items as the correct answer as discussed above.

In this case, a correct (valid) answer consists of items 0, 1, and 3, or Carrots, Cabbage, and Lettuce. (Note that item numbers begin with zero.)

<?xml version="1.0"?>
<exam>
<problem problemNumber="1">
<question>
Which of the following are grown 
in a vegetable garden?
</question>
<answer type="multiple" 
        numberChoices="4" 
        valid="0,1,3">
<item>Carrots</item>
<item>Cabbage</item>
<item>Apples</item>
<item>Lettuce</item>
</answer>
<explanation>
Carrots, cabbage, and lettuce are grown in
a vegetable garden. Apples are grown in 
an orchard.
</explanation>
</problem>

The second problem, as shown below, is a multiple-choice question requiring the selection of a single item as the valid answer.

In this case, there are three choices and the correct answer is item 1. The choices would appear in a properly rendered presentation of the XML file as:

(Note that this is a question about XML entities that illustrates the use of XML entities in the XML file containing the question. How's that for being circular?)

<problem problemNumber="2">
<question>
Which one of the following requires 
XML entities?
</question>
<answer type="single" 
        numberChoices="3" 
        valid="1">
<item>Tom</item>
<item>&quot;&lt;Mary &amp; Sue&gt;&quot;</item>
<item>Dick</item>
</answer>
<explanation>
Angle brackets, ampersands, and quotation 
marks must be represented in XML by 
entities
</explanation>
</problem>

</exam>

additional comments

I hope that by now you appreciate the value of using XML in this situation. Obviously, we could produce such an examination using HTML. However, that would probably require a separate HTML file as well as complex navigation features for each different problem. While this might not be difficult to achieve the first time around (for someone who knows how to create HTML files), maintenance and updating would not be particularly easy.

And what about those teachers who don't know how to create HTML? Despite the availability of WYSIWYG HTML editors that work OK most of the time, things would still be difficult because they don't work OK all of the time. Without having enough knowledge of HTML to correct the problems caused by the WYSIWYG HTML editors, it would not be practical for a teacher to maintain a large number of complex examinations created as HTML files.

Also, it would be difficult to automatically score examinations maintained as HTML files.

On the other hand, since XML separates content from presentation, that same teacher should have no difficulty using a fairly simple GUI editor to maintain the content of an examination formatted as an XML file.

Assuming that the teacher has access to a program that deals adequately with the administration of the exam, including presentation and scoring, the teacher wouldn't have to worry about presentation issues. The teacher would be free to concentrate on content.

coming attractions...

My plan is to continue this discussion in the next article, showing you some of the Java code that can be used to convert the XML file into a set of Java objects using SAX.

the XML octopus

Trying to wrap your brain around XML is sort of like trying to put an octopus in a bottle. Every time you think you have it under control, a new tentacle shows up. XML has many tentacles, reaching out in all directions. But, that's what makes it fun. As your XML host, I will do my best to lead you to the information that you need to keep the XML octopus under control.

credits

This HTML page was produced using the WYSIWYG features of Microsoft Word 97. The images on this page were used with permission from the Microsoft Word 97 Clipart Gallery.

270820

Copyright 2000, Richard G. Baldwin

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

baldwin@austin.cc.tx.us
Baldwin's Home Page

-end-