Java JAXB继承&x2B;XMLAdapter(HasMap)

Java JAXB继承&x2B;XMLAdapter(HasMap),java,xml,inheritance,jaxb,hashmap,Java,Xml,Inheritance,Jaxb,Hashmap,我正在制作一个应用程序,它将包含XML文件中的数据 我现在遇到了一个问题:JAXB不整理我的子类,所以当我整理XML文件时,所有对象都是父类的对象。 我尝试了一些与@xmlseea,AccessType.FIELD和 JAXBContext JAXBContext=JAXBContext.newInstance(Important.class、Parent.class、Child.class) ,但似乎我弄错了什么,而且不起作用 你能给我一些建议吗?我应该使用什么注释?还是mb XMLAdapt

我正在制作一个应用程序,它将包含XML文件中的数据

我现在遇到了一个问题:JAXB不整理我的子类,所以当我整理XML文件时,所有对象都是父类的对象。 我尝试了一些与
@xmlseea
AccessType.FIELD
JAXBContext JAXBContext=JAXBContext.newInstance(Important.class、Parent.class、Child.class)
,但似乎我弄错了什么,而且不起作用

你能给我一些建议吗?我应该使用什么注释?还是mb XMLAdapter? 我的项目的当前结构是(我试图简化它):

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
}
Child.java

public class Child extends Parent {
    public int childField;
    //constructors
}
和简单的JAXB类。 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.");
        }
    }
}
但在编组后,它返回XML,XML不包含任何关于子对象的信息

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

0
1.
0
2.

然后是结束标记

我的地图100%包含不同类型的类:父类和子类


有什么想法吗?

注释让JAXBContext了解您的子类

@xmlseeala(Child.class)
添加到您的
父类
应该可以实现这一目的

有关参考信息,请参见已接受的答案

提议的解决办法

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

    public int parentField;

    public Parent(Integer parentField) {
        this.parentField = parentField;
    }
}
结果让我进了

<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>

0
1.
1.
2.
3.

注释让JAXBContext了解您的子类

@xmlseeala(Child.class)
添加到您的
父类
应该可以实现这一目的

有关参考信息,请参见已接受的答案

提议的解决办法

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

    public int parentField;

    public Parent(Integer parentField) {
        this.parentField = parentField;
    }
}
结果让我进了

<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>

0
1.
1.
2.
3.

wtf。。。它起作用了。我不知道我以前做错了什么,但现在它起作用了。顺便说一句,由于一些问题,我删除了注释@XMLAccessorType。。。它起作用了。我不知道我以前做错了什么,但现在它起作用了。顺便说一句,由于一些问题,我删除了annotation@XMLAccessorType