[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: [jibx-users] How to: abstract mappings with interface implementations? Dave Schoorl Fri Feb 03 18:10:54 2012

Thanks. I already was afraid JiBX needs to "know" which of B or C it's 
unmarshalling at the time it sees the <Identifier1> element. 

However, I am looking into creating my own IUnmarshaller implementation that 
will probe the xml to see what implementation class the xml will resolve to. It 
is my goal to make it a general purpose IUnmarshaller. If it leads to anything 
useful, I will post my solution in this thread.



On Feb 1, 2012, at 4:56 PM, Archie Cobbs wrote:

> On Wed, Feb 1, 2012 at 9:31 AM, Dave Schoorl wrote:
> Thanks for your reply. Actually, I need mapping for classes B and C to be 
> abstract, because in the xml, I don't know what the containing element will 
> be: it could be either Identifier1 or Identifier2 or to turn it around, in 
> the xml, Identifier1 and Identifier2 could be unmarshalled to either an 
> instance of class B or C, depending on the containing xml structure.
> To illustrate this, the following snippets of xml should both be 
> unmarshallable, the first would unmarshal to an instance of class B, the 
> second to an instance of class C.
>   <Identifier1>
>     <LdapName>1</LdapName>
>   </Identifier1>
>   <Identifier1>
>     <IdentPart1>a</IdentPart1>
>     <IdentPart2>b</IdentPart2>
>   </Identifier1>
> To my understanding, this can't work, because JiBX needs to "know" which of A 
> or B it's unmarshalling at the time it sees the <Identifier1> element. By the 
> time it recurses and starts decoding the nested elements, it's already chosen 
> which outer concrete binding it is decoding with.
> Your only option if the XML can't be changed is to create a single class that 
> contains both of A's and B's fields. You could then add methods to that class 
> that returned an actual instance of A or B as appropriate (by 
> copying/mirroring the field values). This adds an ugly extra step in your 
> Java code but allows this XML to work.
> -Archie
> -- 
> Archie L. Cobbs

Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
jibx-users mailing list