advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Java to XML and Back Again with Castor XML :

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>

How to Add Java Applets to Your Site

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.

 BlackBerry Application Development Resources
 Microsoft Visual Studio 2010 Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow
Facebook Makes Major PHP Push With HipHop
Free Ride Over for Microsoft Azure Users
Drupal Opens the Garden to Boost CMS
Oracle Talks Plans for Linux, Solaris
Azure Makes Cloud Computing Innovation Safe
Red Hat's JBoss Looks Ahead
Microsoft Readies Two Windows Phone Systems?

Apple Surveying iPhone Developers? Happiness With The App Store
HTML 5 Leaves Client Storage Open to Web Attacks
Basic Market Forecasting with Encog Neural Networks
Location-Aware App Review
The Future of Web Content -- HTML5, Flash, and Mobile Apps
Moonlight 3.0 Preview Offered For Rich Internet Apps on Linux and Unix
Why a Moderator is Key in the Engineering Review Process, Part II
Windows 7 Features Your Clients Will Need on Day One
What Your Clients Will Ask About Windows 7
Melissa Data Helps Developers Improve the Quality of Business Data

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers