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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer 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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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