Sumanta-
FYI, even if the class isn't in a mapping file, Castor will attempt to
introspect it. This doesn't always produce the prettiest XML, but it
does try to keep the object model fully populated.
I'm glad to help.
Stephen
Sumanta Ranjan Das wrote:
Hi Stephen,
Thanks a lot for the response.
Actually it turned out to be quite simple and easy based on your mapping
file example. I was trying to play around the type attribute for the
holderObj field and hence was not able to make the stuff work. So as it
looks like, Castor automatically does an instance type check whenever it
comes across Object type and if the corresponding class instance mapping
is provided in the mapping file, it resolves it correctly.
Thanks a lot again. Appreciate your help.
Regards,
Sumanta
-----Original Message-----
From: Stephen Bash [mailto:[EMAIL PROTECTED]
Sent: Monday, December 19, 2005 7:19 PM
To: [email protected]
Subject: Re: [castor-user] How to dynamic object resolution in Castor
XML mapping files
Sumanta-
This type of behavior is actually quite easy with Castor. I used your
Container class and created two dummy classes, ClassA and ClassB (one
contains a string, the other a double). I then wrote up a mapping file
(see below), and a driver that creates two containers, puts them in an
ArrayList, marshalls them out and unmarshalls them back in.
Here's the mapping file:
<mapping>
<class name="Container">
<map-to xml="container"/>
<field name="containerId" type="string">
<bind-xml name="id" node="attribute" />
</field>
<field name="holderObj">
<bind-xml name="child" />
</field>
</class>
<class name="ClassA">
<field name="str1" type="string" direct="true">
<bind-xml name="string" node="attribute" />
</field>
</class>
<class name="ClassB">
<field name="dbl1" type="double" direct="true">
<bind-xml name="pi" node="attribute" />
</field>
</class>
</mapping>
Just a note, the class names need to be fully qualified (I cheated in
this case). And here's the output xml:
<?xml version="1.0" encoding="UTF-8"?>
<array-list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<container id="Container1">
<child string="Class A String" xsi:type="class-a"/>
</container>
<container id="Container2">
<child pi="3.141592653589793" xsi:type="class-b"/>
</container>
</array-list>
Castor correctly unmarshalled the XML into objects with no problem. If
you want the xml to change based on the "contained" classes, you can
look into the auto-naming attribute (bind-xml element) in the mapping
documentation:
http://castor.codehaus.org/xml-mapping.html
HTH,
Stephen
Sumanta Ranjan Das wrote:
Hi,
I have my object hierarchy that represents a container which in turn
can
hold any type of other uder-defined objects. e.g. the following
Container object looks like
public class Container {
private String containerId ;
private Object holderObj ;
public String getContainerId()
{
return containerId ;
}
public void setContainerId(String id)
{
containerId = id ;
}
public Object getHolderObj()
{
return holderObj ;
}
public void setHolderObj(Object obj)
{
holderObj = obj;
}
}
I have 2 holder objects (A and B) which are altogether different in
terms of relationship.
In one case when i create Container object, i could do
container.setHolderObj (A aObject)
whereas in other case, i would be doing
container.setHolderObj (B bObject)
In such a case how do i define the mapping file for Container class as
there is no explicit reference to either of A or B object in the
Container class. All i know is that Container can contain either of A
or
B during the runtime.
Does castor XML support such object structures? Can anyone share an
example for the mapping file in case it's already supported?
Thanks and regards,
Sumanta
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------