tags:

views:

281

answers:

1

I have this class model:

abstract class A {
    int a;
}

class B extends A {
    int b;
}

class C extends B {
    int c;
}

And I'd like to get jibx to output this XML:

<B b=1 a=0>
    <children>
     <C c=2 b=1 a=0/>  
    </children>
</B>

I have this binding xml:

<binding>
    <mapping class="A" abstract="true">
     <value name="a" field="a" style="attribute" usage="optional"/>  
     <collection field="children" type="java.util.ArrayList"/>
    </mapping>
    <mapping name="B" class="B" extends="A">
     <value name="b" field="b" style="attribute" usage="optional"/>
     <structure map-as="A"/>
    </mapping>
    <mapping name="C" class="C" extends="B">
     <value name="c" field="c" style="attribute" usage="optional"/>
     <structure map-as="B"/>
    </mapping>
</binding>

However I keep getting artifacts like this:

<C c=2>
    <B b=1 a=0>
     <children>
      ...
     </children>
    </B>
</C>

As temporary solution I've changed my inheritance structure to have AbstractB and B extends AbstractB and C extends AbstractB, but it really annoys me to have to redesign my class because of jibx.

Anyone knows how to solve this?

Edit: As a bonus question - how do you use code/decode java.util.Map with Jibx? I know it can't be done natively (would be glad to be disproved!) but what would you do to code Map (no strings). Please note we're not using jibx-extras.jar, so solutions should not rely on it.

A: 

Actually IMHO it feels kind of normal to get C as parent (in XML sense) of B, because C contains the information of B (from wich it inherits) and its own information aside, but B doesn't know about those C-specific information right ?

To make it clearer: it B is Bird and C is Chicken. Chicken is a Bird, so C inherits form B, OK. But in XML format, would store :

<Bird color="brown">
    <Chicken label="Kentucky-fried" />
</Bird>

or

<Chicken label="Kentucky-fried"> <!-- chicken-specific information -->
    <Bird color="brown" /> <!-- "birdy" part of the chicken -->
</Chicken>

?

So on the model point of view, it seems logical to me... And I didn't find a way of achieving the opposite in the binding tutorial, sorry.

streetpc
thanks for the effort, but that answer doesn't really solves my problem.
Ran Biron