Adding Collection to the POJO
When you start implementing this technology in real-life, you will need to use Collections in your POJO object. First, modify the POJO:
private List myList = new ArrayList();
public void setMyList(List myList) {
this.myList = myList;
}
public List getMyList() {
return this.myList;
}
public Person(String name, String email, String phone, List myList) {
this.name = name;
this.email = email;
this.phone = phone;
this.myList = myList;
}
However, to marshal and unmarshal the class with Collections inside, you need to create an XML mapping for it (XML mapping is very well documented in the Castor XML mapping manual). There are also different tools which automate mapping creating from your class. However, creating mapping without any tools is rather easy. First, create a new file called mapping.xml:
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<class name="us.prokhorenko.jx.Person">
<map-to xml="person"/>
<field name="email" type="string">
<bind-xml name="email" node="element"/>
</field>
<field name="phone" type="string">
<bind-xml name="phone" node="element"/>
</field>
<field name="name" type="string">
<bind-xml name="name" node="element"/>
</field>
<field name="myList" type="java.util.List"
collection="arraylist">
<bind-xml name="myList" node="element"/>
</field>
</class>
</mapping>
Describing XML mapping goes beyond the topic of this article, but you can see it's pretty straightforward. Simply list the fields in the class and bind them to the appropriate XML elements (or attributes).
After the class has got Collection and you've got a mapping for it, you need to apply the mapping. First, modify the test application where it marshals the class to XML (the modifications are marked in bold):
// Marshalling class to XML
List myList = new ArrayList();
l.add("List#1");
l.add("List#2");
// Create the Person class
Person person = new Person("Mr. White", "mr@white", "626-555-1234", myList);
// We have a mapping, apply it
Mapping mapping = new Mapping();
mapping.loadMapping("mapping.xml");
// Marshal and save to XML file
FileWriter file = new FileWriter("person.xml");
Marshaller m = new Marshaller(file);
m.setMapping(mapping);
m.marshal(person);
file.close();
Next, modify the code where it unmarshals the object from XML:
// Unmarshalling XML to class
// Read from XML and unmarshal
FileReader uFile = new FileReader("person.xml");
Unmarshaller u = new Unmarshaller(mapping);
Person uPerson = (Person)u.unmarshal(uFile);
// Show name and email
System.out.println("name: " + uPerson.getName());
System.out.println("email: " + uPerson.getEmail());
List myList = new ArrayList();
myList = uPerson.getMyList();
System.out.println("myList[0]: " + (String)myList.get(0));
The output XML file (person.xml) changes accordingly:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<myList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="java:java.util.String">
List#1
</myList>
<myList xsi:type="java:java.util.String">
List#2
</myList>
<email>prokhorenko@gmail.com</email>
<name>Olexandr Prokhorenko</name>
<phone>123-555-1234</phone>
</person>
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|