[英]JAXB Inheritance + XMLAdapter (HasMap)
I am making an app, that will contain data in XML file. 我正在制作一个应用程序,它将包含XML文件中的数据。
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. 我现在遇到一个问题:JAXB不会编组我的子类,因此当我umarshall XML文件时,所有对象都是Parent类的对象。 I tried some variations with
@XMLSeeAlso
, AccessType.FIELD
and JAXBContext jaxbContext = JAXBContext.newInstance(Important.class, Parent.class, Child.class);
我尝试了
@XMLSeeAlso
, AccessType.FIELD
和JAXBContext jaxbContext = JAXBContext.newInstance(Important.class, Parent.class, Child.class);
一些变体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?
或MB XMLAdapter? The current structure of my project is (I tried to simplify it):
我项目的当前结构是(我试图简化它):
Main.java 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 Important.java
@XmlRootElement(name="important")
public class Important {
public Map<Integer, Parent> hashMap;
//some code here
}
Parent.java Parent.java
public class Parent{
public int parentField;
//constructor
}
Child.java Child.java
public class Child extends Parent {
public int childField;
//constructors
}
And simple JAXB class. 和简单的JAXB类。 JAXB.java
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. 但是,在将其编组后返回XML,其中不包含有关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 我的地图100%包含不同类型的类别:父级和子级
Any thoughts? 有什么想法吗?
The @XmlSeeAlso
annotation lets the JAXBContext know about your subclasses. @XmlSeeAlso
批注使JAXBContext知道您的子类。
Adding @XmlSeeAlso(Child.class)
to your Parent
class should do the trick. 将
@XmlSeeAlso(Child.class)
添加到您的Parent
类应该可以解决问题。
For reference, see also the accepted answer for Using JAXB to pass subclass instances as superclass . 作为参考,另请参见使用JAXB将子类实例作为超类传递的公认答案。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.