Since the Party object can contain a Person or a Business object, wouldn't it be better modeled if you have a abstract base class for these objects? Then Party object would contain a reference to this base type. In Jibx, you would then have a abstract mapping for the base type, mappings for each inherited type and a mapping for the Party type. This approach assumes that you have the liberty to define the input xml and that you can change your model classes.
Varghese [EMAIL PROTECTED] wrote: > > Hello All, > > I have a question about unmarshalling to specific objects based off an > attribute value in the input xml. At any given time, my input xml > tag will remain the same. However, the *xsi:type* attribute could be > different. I need to map/bind to a specific object based off the > value in the attribute. I have not found anywhere in the > documentation where I can bind to a specific object dynamically, but I > have been trying to get this to work using method hooks. > . > For example, my input xml will contain one of the following entries.... > > <ns:Party xsi:type='person'> > <ns:Id>1111111111111111111</ns:Id> > <ns:CommonType>P</ns:CommonType> > <ns:BirthDate>P</ns:BirthDate> > </ns:Party> > > or > > <ns:Party xsi:type='business'> > <ns:Id>22222222222222222</ns:Id> > <ns:CommonType>O</ns:CommonType> > <ns:BusinessType>1</ns:BusinessType> > </ns:Party> > > > My binding definition contains the following.... > > <mapping name="Party " class="commonparty.Party" post-set="postset" > ordered="false"> > <namespace prefix="obj" uri="http://input.common.com" > default="elements"/> > <namespace prefix="xsi" > uri="http://www.w3.org/2001/XMLSchema-instance" default="attributes"/> > <value name="type" field ="type" style="attribute" > usage="optional" /> > <value name="Id" field="partyId" usage="optional"/> > <value name="CommonType" field="commonType" usage="optional"/> > </mapping> > > > My java class structure declares that a Party object is composed of a > Person and Business object. However, at any give time the Party > object would contain a reference to either Person or Business. > > public class Party { > public String type; > public Person person; > public Business business; > > public void postset(IUnmarshallingContext ctx) { > if ( type.equals("Person") ) { > //Copy fields from Party to new instance of > Person > //Replace Party object on the stack with Person > > } else { > //Copy fields from Party to new instance of > Business > //Replace Party object on the stack with of > } > } > . > . > . > } > > public class Person { > public String birthDate; > . > . > . > } > > public class Business{ > public String businessType; > . > . > . > } > > > As you can see, I am always mapping the "Party" tag to a Party object. > The party object contains a post-set method that accepts the > IUnmarshallingContext signature. In the postset method, I am > evaluating the xsi:type. If the type is "Person", then I want to pop > the current Party object from the stack and push a new instance of a > Person object for JiBX to unmarshall. After coming to this idea, I > realized that I do not have a specific mapping for a Person object. I > definitly need a Person and a Business object because they contain > different fields ( as listed in my input example above ). > > Does anyone know if there is a binding syntax that will allow me to > handle this work in the binding.xml? If not, is there another > solution without modifying JiBX? > > Thanks for the help! > > Barry > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > ------------------------------------------------------------------------ > > _______________________________________________ > jibx-users mailing list > jibx-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/jibx-users > ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ jibx-users mailing list jibx-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jibx-users