Hi,

I have used JiBX for simple XML mappings, but now I get to a more complex
mapping involving a collection of subclasses of one super class. I need to
map these subclasses to the same element name. With the mapping below I
can create more or less the correct structure, but the item tag names will
be item1, item2 and item3 instead of item for all of the subclasses.
When I use the same name for all of them I get the compile error
"Duplicate mapping name not allowed for unmarshalling".

Is there a way to have JiBX output the XML with the subclasses mapped to
the same tag name?
NB: there is no need to go from XML back to objects.

Regards,
Frank van Eerden

These are the classes:

class BaseType {
    long id;
}
class StringType extends BaseType {
    String key;
    String value;
}
class DictType extends BaseType {
    Set<BaseType> items;
}
class ListType extends BaseType {
    String key;
    Set<BaseType> items;
}
class Message {
    String name;
    Set<BaseType> fields;
}

The XML the JiBX mapping should be able to generate:

<message>
    <name>value</name>
    <fields>
        <item><key>name1</key><valueString>value1</valueString></item>
        <item><key>name2</key><valueString>value2</valueString></item>
        <item>
            <key>name3</key>
            <valueList>
                <item>
                    <valueDict>
                        
<item><key>name3.1</key><valueString>value3.1</valueString></item>
                        
<item><key>name3.2</key><valueString>value3.2</valueString></item>
                    </valueDict>
                </item>
            </valueList>
        </item>
    </fields>
</message>

The binding file used:

<binding>
        <mapping name="message" class="package.Message">
                <value name="name" field="name" />
                <collection name="fields" field="params" 
type="java.util.HashSet">
                        <structure map-as="package.BaseType" />
                </collection>
        </mapping>

        <mapping name="base" class="package.BaseType" abstract="true"/>
        <mapping name="item1" class="package.TypeString" 
extends="package.BaseType">
                <value name="key" field="key" />
                <value name="valueString" field="value" />
        </mapping>
        <mapping name="item2" class="package.TypeList" 
extends="package.BaseType">
                <value name="key" field="key" />
                <collection name="valueList" field="items" 
type="java.util.HashSet">
                        <structure map-as="package.BaseType" />
                </collection>
        </mapping>
        <mapping name="item3" class="package.TypeDict" 
extends="package.BaseType">
                <collection name="valueDict" field="items" 
type="java.util.HashSet">
                        <structure map-as="package.BaseType" />
                </collection>
        </mapping>
</binding>

The End



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to