E
E
Eugene2013-12-02 12:36:23
Java
Eugene, 2013-12-02 12:36:23

How does inheritance work in classes for JAXB xml serialization?

Hello.
Tell me how inheritance works in classes for JAXB xml serialization.
Those. i have class MySuperXml:

@XmlRootElement(name = "super-xml")
@XmlSeeAlso(OtherSuperXml.class)
public class MySuperXml {
     private List<Long> numbers;
 }

It has a child OtherSuperXml:
public class OtherSuperXml extends MySuperXml {
     private List<String> names;
 }

When mapping, I get an empty list of numbers, although they are physically present in xml. How to map parent fields (if I understand the task correctly)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Lopatin, 2013-12-02
@zolt85

They were probably just added to names. Still don't want to add annotations for numbers and names? I don't even know how it will be represented in XML without them.
For example, this is how you can do it:

@XmlRootElement(name = "super-xml")
@XmlSeeAlso(OtherSuperXml.class)
public class MySuperXml {
     @XmlElement(name = "number")
     private List<Long> numbers;
}
@XmlRootElement(name = "other-super-xml")
public class OtherSuperXml extends MySuperXml {
    @XmlElement(name = "name")
    private List<String> names;
}

In general, it is difficult to answer something on a fictional example. You are clearly using a different XML schema...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question