如何从XML中的子类元素获取java中的基类引用
我有一个XML,它看起来像如何从XML中的子类元素获取java中的基类引用,java,json,xml,jaxb,unmarshalling,Java,Json,Xml,Jaxb,Unmarshalling,我有一个XML,它看起来像 <Record> <Student> <name>sumit</name> <rollno>123</rollno> <Student> <Record> 现在在我的应用程序中,我从XML创建对象,如下所示 InputStream inputStream = new FileInputStream("/home/sumit/abc.xml"); J
<Record>
<Student>
<name>sumit</name>
<rollno>123</rollno>
<Student>
<Record>
现在在我的应用程序中,我从XML创建对象,如下所示
InputStream inputStream = new FileInputStream("/home/sumit/abc.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Record.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Record data = (Record) jaxbUnmarshaller.unmarshal(inputStream);
但是我在data.getPerson()中得到了null
谁能帮我一下我做错了什么。这不是你想要的方式。XML元素由标记名标识,标记名需要与Java类中的字段名相对应。一旦
被确定为类记录的值,
在该类的字段中没有对应项,JAXB无法解组该内容
使用此修改的类将
更改为
)后,您应该能够解组8:
class Record{
private Student student;
public Student getStudent(){
return student;
}
public void setStudent(Student value){
student = value;
}
}
(请注意,由于类记录中Person字段的类型字符串与Person不匹配,还有另一个问题。)
class Record{
private Student student;
public Student getStudent(){
return student;
}
public void setStudent(Student value){
student = value;
}
}