XML Data Binding with Castor
Pages: 1, 2
Developing data-binding for Address Book
We define the address book as holding a collection of persons. To build this, we will have to create a mapping for the data-binding framework, and we will see how it will be able to set up a java.util.Collection of Persons. We will also learn about mapping attributes (as in <foo anAttribute="somevalue">).
Here are the steps we will follow:
- Create an XML representation of the address book
- Create a Java object representing the address book
- Create a
mapping.xmlfile to allow Castor XML to marshal and unmarshal - Use Castor XML to display the address book
Step One: Create an XML representation of the address book
Our address book will have an <addressbook> tag wrapping around the <person> elements that we defined earlier:
<addressbook name="Manchester United Address Book">
<person name="Roy Keane">
<address>23 Whistlestop Ave</address>
<ssn>111-222-3333</ssn>
<email>roykeane@manutd.com</email>
<home-phone>720.111.2222</home-phone>
<work-phone>111.222.3333</work-phone>
</person>
<person name="Juan Sebastian Veron">
<address>123 Foobar Lane</address>
<ssn>222-333-444</ssn>
<email>juanveron@manutd.com</email>
<home-phone>720.111.2222</home-phone>
<work-phone>111.222.3333</work-phone>
</person>
</addressbook>
Step Two: Create a Java object representing the address book
Now that we have the address book defined in XML, we need to create the AddressBook object that represents it. We will make the AddressBook hold a java.util.List of Persons:
import java.util.List;
import java.util.ArrayList;
public class Addressbook {
private String addressBookName;
private List persons = new ArrayList();
public Addressbook() { }
// -- manipulate the List of Person objects
public void addPerson(Person person) {
persons.add(person);
}
public List getPersons() {
return persons;
}
// -- manipulate the name of the address book
public String getName() {
return addressBookName;
}
public void setName(String name) {
this.addressBookName = name;
}
}
Now we have the programming interface that we'll use to work with an address book, and we have defined the XML representation. We need to tie these together, and that is where the mapping file comes in.
Step Three: Create a mapping.xml file to allow Castor XML to marshal and unmarshal
In our simple example of a person, we didn't have to create a mapping file, because Castor could work out what to do based on the JavaBean conventions that we followed.
We have to use the mapping file now, however, as we need to instruct Castor that:
- We are going to have a collection of
persons - We changed the
<person>element to contain the name as an attribute - We are using an attribute to name the address book
Here is the mapping file (mapping.xml):
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN" "http://castor.exolab.org/mapping.dtd">
<mapping>
<description>A mapping file for our Address Book application</description>
<class name="Person">
<field name="name" type="string">
<bind-xml name="name" node="attribute" />
</field>
<field name="address" type="string" />
<field name="ssn" type="string" />
<field name="email" type="string" />
<field name="homePhone" type="string" />
<field name="workPhone" type="string" />
</class>
<class name="Addressbook">
<field name="name" type="string">
<bind-xml name="name" node="attribute" />
</field>
<field name="persons" type="Person" collection="collection" />
</class>
</mapping>
Looking at the mapping file, you will see that it is built from the point of view of the Java class. We have Person.java and Addressbook.java, and both have a <class> element to describe them.
Each <class> has fields. This is where we tell Castor the name of the fields, their type, if they are an element or an attribute, and if there is one of them or a collection.
The following snippet describes the fact that the given tag has an attribute, of type String, with the name name. From this, Castor knows that the XML document will hold <addressbook name="the value">.
<field name="name" type="string">
<bind-xml name="name" node="attribute" />
</field>
The next snippet gives the framework the ability to use a collection (in our case a java.util.List) of persons.
<field name="persons" type="Person" collection="collection" />
You can model many things in this mapping file, but let's not digress. For extensive coverage of this mapping file, see the Castor Web site.
Step Four: Use Castor XML to display the address book
To display the address book, we will want to unmarshal addressbook.xml. Unlike the simple person.xml, we will have to load the mapping file.
Here is the code for ViewAddressBook.java:
import org.exolab.castor.xml.*;
import org.exolab.castor.mapping.*;
import java.io.FileReader;
import java.util.List;
import java.util.Iterator;
public class ViewAddressbook {
public static void main(String args[]) {
try {
// -- Load a mapping file
Mapping mapping = new Mapping();
mapping.loadMapping("mapping.xml");
Unmarshaller un = new Unmarshaller(Addressbook.class);
un.setMapping( mapping );
// -- Read in the Addressbook using the mapping
FileReader in = new FileReader("addressbook.xml");
Addressbook book = (Addressbook) un.unmarshal(in);
in.close();
// -- Display the addressbook
System.out.println( book.getName() );
List persons = book.getPersons();
Iterator iter = persons.iterator();
while ( iter.hasNext() ) {
Person person = (Person) iter.next();
System.out.println("\n" + person.getName() );
System.out.println("-----------------------------");
System.out.println("Address = "+ person.getAddress());
System.out.println("SSN = " + person.getSsn() );
System.out.println("Home Phone = " +
person.getHomePhone() );
}
} catch (Exception e) {
System.out.println( e );
}
}
}
The only real difference here is that we are no longer using the static Unmarshaller.unmarshal() method. Now we instantiate an Unmarshaller object, and set the mapping to the newly created Mapping() object. The Mapping() object loads the XML mapping file that we just created:
Mapping mapping = new Mapping();
mapping.loadMapping("mapping.xml");
Unmarshaller un = new Unmarshaller(Addressbook.class);
un.setMapping( mapping );
Conclusion
We have shown that working with XML doesn't mean that you have to delve into the books to learn SAX, DOM, JAXP, and all the other TLAs. Castor's XML data-binding provides a simple but powerful mechanism to work with XML and Java objects.
Dion Almaer is a Principal Technologist for The Middleware Company, and Chief Architect of TheServerSide.Com J2EE Community.
Return to ONJava.com.
-
Easier option?
2008-12-18 09:03:03 safepage [View]
-
Need Suggestion to mapping the complex xml
2008-06-09 20:18:08 ranjith1 [View]
-
as I modify an existing Document XML with "Castor"
2007-06-25 15:02:35 HernanNina [View]
-
conditional bind xml attribute
2007-04-10 08:52:42 techijr [View]
-
When reading data from xml using Unmarshaller.unmarshal
2007-03-04 23:30:35 Bollu_Narendra [View]
-
When reading data from xml using Unmarshaller.unmarshal
2007-06-11 01:17:13 yesudas [View]
-
writing data to xl sheet using Marshall class
2007-03-04 23:17:17 Bollu_Narendra [View]
-
writing data to xl sheet using Marshall class
2007-06-11 01:15:10 yesudas [View]
-
Unmarshalling Complex Java Objects
2006-12-15 02:41:41 niti [View]
-
special character value
2006-06-27 20:39:29 sunonepp [View]
-
(URGENT)problem with loading mapping.xml
2006-05-31 22:38:34 rajaraju [View]
-
why cannot i open : http://www.castor.org , always!!??
2006-05-19 01:17:59 bigheadgp [View]
-
How to convert efficienty XML big files
2006-02-19 02:50:03 davidlealvalmana [View]
-
How to convert efficienty XML big files
2006-02-26 13:34:37 glenpepicelli [View]
-
Castor Problem Need Help Thanx
2005-12-19 03:22:39 raghav123 [View]
-
Updated Schemas
2005-12-13 10:57:00 da4thrza [View]
-
annoying error when trying to run example
2005-06-09 10:24:24 jmgreen7 [View]
-
annoying error when trying to run example
2005-08-05 07:14:13 HarienDeMel [View]
-
How to bind all low-level elements in one top level class
2005-05-04 21:34:42 vijaypriya [View]
-
How to bind all low-level elements in one top level class
2005-11-07 00:29:22 Ramdevaki [View]
-
Castor has moved
2005-04-22 15:18:56 ariconsul [View]
-
Thanks
2004-08-04 13:29:41 uhauser [View]
-
a query about mapping.
2004-07-30 13:04:17 arya41 [View]
-
handling enums in mapping xml
2004-04-22 12:57:49 Espinosa [View]
-
Problem with Castor.exolab.org/mapping.dtd
2004-03-25 09:51:33 kmullin [View]
-
Problem with castor.exolab.org/mapping.dtd
2004-03-25 09:38:41 kmullin [View]
-
Problem with castor.exolab.org/mapping.dtd
2006-03-24 15:48:47 MobileX [View]
-
Problem with castor.exolab.org/mapping.dtd
2006-10-12 09:01:54 hsgujral@hotmail.com [View]
-
Problem with castor.exolab.org/mapping.dtd
2004-06-01 23:43:28 Pashmina [View]
-
Problem with castor.exolab.org/mapping.dtd
2006-01-18 09:15:12 rlaplante [View]
-
How to get Address Books from XML file using mapping file
2003-12-01 02:01:11 thong39th [View]
-
How to get Address Books from XML file using mapping file
2004-03-11 13:19:03 starlightpurple [View]
-
How to get Address Books from XML file using mapping file
2007-12-07 01:15:56 AACR [View]
-
How to get Address Books from XML file using mapping file
2003-12-12 06:00:21 anonymous2 [View]
-
How to get Address Books from XML file using mapping file
2003-12-12 06:00:11 anonymous2 [View]
-
How to get Address Books from XML file using mapping file
2003-12-01 18:43:24 anonymous2 [View]
-
Nice for beginers
2003-08-26 12:15:47 anonymous2 [View]
-
Nice for beginers
2003-08-30 07:55:26 anonymous2 [View]
-
Validations checking
2003-07-27 06:12:40 anonymous2 [View]
-
XML Binding
2003-02-24 20:54:13 anonymous2 [View]
-
XML Data Binding with Castor XML
2002-10-29 08:15:14 anonymous2 [View]
-
XML Data Binding with Castor XML
2003-12-01 18:42:19 anonymous2 [View]
-
XML Data Binding with Castor XML
2004-10-01 08:12:01 katiep [View]
-
Data mappings and Exception handling in Castor
2002-10-21 00:00:34 anonymous2 [View]
-
creating an address book using marshal
2002-05-30 05:35:52 howardjfraser [View]
-
creating an address book using marshal
2003-07-23 07:35:54 anonymous2 [View]
-
an article on castor JDO please
2002-02-28 14:09:36 sai001 [View]
-
Marshal error: java.lang.ArrayIndexOutOfBoundsException
2002-02-28 11:36:49 pqrussell [View]
-
thanks
2001-12-27 20:31:47 gnarayanan [View]
-
Good straightforward explanation of the power of Castor
2001-11-07 17:51:52 dhalbrook [View]
-
good, clear explanations
2001-11-02 05:19:52 insync [View]
-
good, clear explanations
2004-04-05 09:26:12 lyl000 [View]