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
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.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.
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.
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: