Hi Bryce,

The easy case is if you only need this to work for unmarshalling, and 
not for marshalling. In this case you can use a binding with 
direction="input" and just define multiple <mapping> definitions for the 
same class, with different element names. The element name actually used 
in the document will determine which <mapping> gets applied, and that 
can just reference the appropriate subclass of A directly. But if you 
need to handle both unmarshalling and marshalling the multiple <mapping> 
approach won't work, since there's no way for JiBX to select the 
appropriate <mapping> when marshalling an instance of Foo (and the 
binding compiler treats it as an error if you try to use multiple 
<mapping>s for the same class in a binding supporting output).

So if you need this to work for marshalling you can't just do it 
directly in the binding definition. You *can* write special 
marshalling/unmarshalling code for Foo to do what you want, and that 
might be a good solution. The type of code you need depends on how this 
is to be used. If the <FooX> element is to be the root element of the 
document, you'd need to write this as front-end code that invokes JiBX 
marshalling/unmarshalling; if it's going to be embedded in other 
elements, you can use a custom marshaller/unmarshaller with no element 
name defined in the binding.

But a better solution may be to restructure something. If you were 
really working with the simple data structure you've shown, my 
suggestion would be to just eliminate the use of he class Foo completely 
and instead define <mapping name="FooB" class="B" ...> and <mapping 
name="FooC" class="C" ...>, since the Foo class is just a wrapper for 
one of the subclasses of A. I assume that your real data is more 
complicated, though, and that this wouldn't work. But perhaps there's 
another way to restructure either your data or your XML representation 
to eliminate some of the indirection.

Hope this gives you some ideas, and feel free to come back with more 
details about the problem.

  - Dennis

Dennis M. Sosnoski
SOA and Web Services in Java
Training and Consulting
http://www.sosnoski.com - http://www.sosnoski.co.nz
Seattle, WA +1-425-939-0576 - Wellington, NZ +64-4-298-6117



Bryce Fosdick wrote:
> If any one can accomplish the following I would appreciate it.
>
> public class Foo {
>       A a;
> }
>
> public abstract class A {
> }
>
> public class B extends A {
> }
>
> public class C extends A {
> }
>
> <binding>
>       <mapping name="FooB" class="Foo">
>               <structure name="B" field="A" choice="true"/>
>               <structure name="C" field="A" choice="true"/>
>       </mapping>
> </binding>
>
> So that <FooB>...</FooB> results in Foo where A is type B, and 
> <FooC>...</FooC> results in Foo where A is type C.
>
> I am trying to avoid something like:
>
> public class Foo {
>       B b;
>       C c;
> }
>
> public abstract class A {
> }
>
> public class B extends A {
> }
>
> public class C extends A {
> }
>
> <binding>
>       <mapping name="FooA" class="Foo">
>               <structure name="B" field="B" choice="true"/>
>               <structure name="C" field="C" choice="true"/>
>       </mapping>
> </binding>
>
>
> <FooA>...</FooA>
> if(fooA.getB() != null) {...}
> else if(fooA.getC() != null) {...}
>
> Hope it makes sense.  Thanks
>
> -------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
> http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
> _______________________________________________
> jibx-users mailing list
> jibx-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jibx-users
>
>   


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to