All_Safe
All_Safe

Reputation: 1399

Using Inheritance with JAXB

I have xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<UserTypes>
    <UserType Id="1">
        <Members>
            <Member UserTypeId="4" Name="Name1" />
            <Member UserTypeId="1" Name="Name2" />
        </Members>
    </UserType>
    <UserType Id="23" ItemUserTypeId="232">
        <Description />
    </UserType>
</UserTypes>

Some elements have specific nested elements and attributes. In Java I created general class and its heirs:

@XmlAccessorType(XmlAccessType.FIELD)
public class UserType {

    @XmlAttribute(name = "Id", required = true)
    private Integer id;

    private UserTypeKind userTypeKindType;
    //getters and setters
}

@XmlAccessorType(XmlAccessType.FIELD)
public class StructUserType extends UserType {

    @XmlElement(name = "Members")
    MembersList membersList =  new MembersList();

    static class MembersList {
        @XmlElement(name = "Member")
        List<Member> members = new ArrayList<>();
    }

    static class Member {
        @XmlAttribute(name = "Name", required = true)
        String name;

        @XmlAttribute(name = "UserTypeId", required = true)
        long userTypeId;
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
public class ListUserType extends UserType {

    @XmlAttribute(name = "ItemUserTypeId", required = true)
    private long itemUserTypeId;

    public long getItemUserTypeId() {
        return itemUserTypeId;
    }

    public void setItemUserTypeId(long itemUserTypeId) {
        this.itemUserTypeId = itemUserTypeId;
    }
}

I want marshall my objects to xml. I do:

List<UserType> userTypes = new ArrayList<>();

StructUserType structUserType = new StructUserType();
smbpStructUserType.setId(1);
SmbpStructUserType.Member member1  = new SmbpStructUserType.Member();
member1.name = "Name1";
member1.userTypeId = 4;
SmbpStructUserType.Member member2  = new SmbpStructUserType.Member();
member2.name = "Name2";
member2.userTypeId = 1;
structUserType.membersList.members.add(member1);
structUserType.membersList.members.add(member2);


ListUserType listUserType = new ListUserType();
listUserType.setId(23);
smbpListUserType.setItemUserTypeId(232);

userTypes.add(structUserType);

But when I try to do marshaling, only the elements that fall into the final xml are defined only in the main class (UserType), from which all are inherited. What is wrong with it? What kind of configuration of JAXB is needed so that the elements of the child classes also fall into the final xml?

Upvotes: 1

Views: 722

Answers (1)

Alex A.
Alex A.

Reputation: 39

There're a few option I think. The easiest is give to your UserType tags two different name like StructUserType and ListUserType. Then you need to specify which name suitable for which class in root element

@XmlElements({
            @XmlElement(name = "StructUserType", type = StructUserType.class),
            @XmlElement(name = "ListUserType", type = ListUserType.class)
    })
    private List<UserType> userTypse;

Another option is use @XmlJavaTypeAdapter(YourAddapter.class), extend XmlAdapter class in you YourAddapter class and override marshal method where you can specify how should be marshaled you UserType tag.

Upvotes: 1

Related Questions