jmkgreen
jmkgreen

Reputation: 1643

Two utterly different JAXB annotated classes, same @XmlElement name. Possible?

We have an existing document tree. We want to wrap some of the elements inside this tree up within a element.

Depending on where in the tree we are, the element will hold very different content.

So I have a DocumentPromptLanguage class and a DocumentRouterLanguage class. They have different parents and different children but it makes sense that in XML they are both called <language>.

Is this possible without adapters or must the XML representation disambiguate by element name?

Sample:

<?xml version="1.0" encoding="utf-8"?>
<doc>
    <info>
        <language>
            <iso639>en</iso639>
            <value>This is a sample document</value>
        </language>
        <language>
            <iso639>es</iso639>
            <value>Se trata de un documento de muestra</value>
        </language>
    </info>
    <someElement>
        <route>
            <language>
                <iso639>en</iso639>
                <possibleValues>Yes|No|Maybe</possibleValues>
                <prefix>For</prefix>
            </language>
            <language>
                <iso639>es</iso639>
                <possibleValues>sí|not|tal vez</possibleValues>
                <prefix>para</prefix>
            </language>
            <when>Tuesday</when>
            <afterTime>17.30</afterTime>
            <goto></goto>
        </route>
    </someElement>
</doc>

Upvotes: 1

Views: 388

Answers (1)

bdoughan
bdoughan

Reputation: 149017

Yes the class mapped to the route element can have a property mapped with @XmlElement(name="language"), and so can the class mapped to the info element. This because the mappings are scoped by class.

Upvotes: 1

Related Questions