Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Reading objects is easy with SAX

Write a reusable ContentHandler for reading objects from XML

  • Print
  • Feedback
Application data should be held in a database or in configuration files, but never hardcoded in the application itself. Developers don't need to think about a configuration file format: XML is the preferred choice. The SAX and DOM API implementations help us read XML files. DOM is a tree-based datastructure built from an XML file; SAX presents the document as a sequence of events (it reports every time it encounters begin and end tags). For handling those events, we must implement a ContentHandler and register it to the org.xml.sax.XMLReader, which reads the XML file. In this article, we will design a component that handles the repetitious task of creating objects from configuration files.

Mapping objects to XML

In this section, we will see how objects must be mapped to XML in order to use a generic ContentHandler, from where the object will be created. First we will look at the definition of field values. Then we will see how the associations between the objects are defined. Finally, I will demonstrate the class model, which provides the whole infrastructure to create and initialize the objects from XML easily.

Field values

In XML, you can express a value as an attribute or an element. This allows you to express an object's field values in different ways. Here are four suggestions for mapping object Person, with the fields firstName and lastName, to XML:

Suggestion 1:

<person id="1" firstName="Nick" lastName="Kassem"/>


Suggestion 2:

<person id="1">
   <fields firstName=" Nick" lastName="Kassem"/>
</person>


Suggestion 3:

<person type=" test.person" id="1">
   <firstName> Nick </firstName>
   <lastName>Kassem</lastName>
</person>


Suggestion 4:

<object type=" test.person" id="1"> 
<field name="firstName"> Nick </field> 
<field name="lastName"> Kassem</field> 
</object>


Besides the field values, I added the attribute id and/or the attribute type to each suggestion. (id represents an object identity; type represents the object type.) Each object representation would probably work fine, but I prefer the fourth approach because of a rule I found in Brett McLaughlin's Java and XML (see Resources for a link):

Although there is no specification or widely accepted standard for determining when to use an attribute and when to use an element, there is a good rule of thumb: use elements for presentable data and attributes for system data.


Applying that rule helps us separate system data from user data; this improves XML files' readability. When we deal with user data, we can concentrate on elements and ignore the attribute values.

Associations

Objects contain not only fields, but also associations for linking objects. To express an association, you can follow the approach used in DTDs with attribute type IDREFS:

<!ELMENT person (firstName, lastName)>
<!ELMENT personList (listName)>
<!ATTLIST person id ID #required>
<!ATTLIST personList refPersons IDREFS #implied>
<person id="1">
   <firstName>firstName1</firstName>
   <lastName>lastName1</lastName>
</person>
<person id="2">
   <firstName>firstName2</firstName>
   <lastName>lastName2</lastName>
</person>
<personList refPersons="1 2">
   <listName>listName1</listName>
</personList>


By adding all referenced object ids to one string, we are able to express 1:n and n:m associations. You can express the ids for associations in either attributes or elements. For readability's sake, I suggest always using the same approach. (I will use elements here.) I will use two more objects -- Communication and PersonList -- to illustrate how this works:

  • Print
  • Feedback
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?

Resources