简体   繁体   中英

JAXB Inheritance + XMLAdapter (HasMap)

I am making an app, that will contain data in XML file.

I stuck right now with one problem: JAXB do not marshall my child class, so when I umarshall XML file, all objects are objects of Parent class. I tried some variations with @XMLSeeAlso , AccessType.FIELD and JAXBContext jaxbContext = JAXBContext.newInstance(Important.class, Parent.class, Child.class); , but it seems like I missunderstand something, and it doesn't work.

Could you give me some advices? What annotations should I use? or mb XMLAdapter? The current structure of my project is (I tried to simplify it):

Main.java

public class Main {
    public static void main(String args[]){
        JAXB jaxb = new JAXB();
        Parent parent = new Parent(1);
        Child child = new Child(2,3)
        Important important = new Important();
        jaxb.write(important);
    }
 }

Important.java

@XmlRootElement(name="important")
public class Important {
public Map<Integer, Parent> hashMap;
    //some code here
}

Parent.java

public class Parent{
    public int parentField;
    //constructor
}

Child.java

public class Child extends Parent {
    public int childField;
    //constructors
}

And simple JAXB class. JAXB.java

public class JAXB {
    public void write(Important important) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Important.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(important, System.out);

        } catch (JAXBException e) {
            System.err.println(e + "Error.");
        }
    }
}

But after marshalling it returns XML, that doesn't contain any information about child.

<important>
   <hashMap>
       <entry>
           <key>0</key>
           <value>
               <parentField>1</parentField>
           </value>
       </entry>
       <entry>
           <key>0</key>
           <value>
               <parentField>2</parentField>
           </value>
       </entry>
   </hashMap>

and then closing tag.

My map 100% contains different types of classes: Parent and Child

Any thoughts?

The @XmlSeeAlso annotation lets the JAXBContext know about your subclasses.

Adding @XmlSeeAlso(Child.class) to your Parent class should do the trick.

For reference, see also the accepted answer for Using JAXB to pass subclass instances as superclass .

Proposed solution

@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso(Child.class)
public class Parent {

    public int parentField;

    public Parent(Integer parentField) {
        this.parentField = parentField;
    }
}

Results for me into

<important>
    <map>
        <entry>
            <key>0</key>
            <value>
                <parentField>1</parentField>
            </value>
        </entry>
        <entry>
            <key>1</key>
            <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="child">
                <parentField>2</parentField>
                <childField>3</childField>
            </value>
        </entry>
    </map>
</important>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM