2016-03-23 57 views
0

我有一个XML看起来像如何获得

<Record> 
    <Student> 
    <name>sumit</name> 
    <rollno>123</rollno> 
    <Student> 
<Record> 

和模型类在Java中,从儿童类元素在XML基类引用看起来像

class Record{ 
    @JsonProperty("person") 
    private Person person; 
    public String getPerson(){ 
     return person; 
    } 
    public void setPerson(String person){ 
     this.person=person; 
    } 
} 

abstract class Person{ 
    @JsonProperty("name") 
    private String name; 
    public String getName(){ 
     return name; 
    } 
    public void setName(String name){ 
     this.name=name; 
    } 
} 

@JsonTypeName("Student") 
class Student extends Person{ 
    @JsonProperty("rollno") 
    private String rollno; 
    public String getrollno(){ 
     return rollno; 
    } 
    public void setName(String rollno){ 
     this.rollno=rollno; 
    } 
} 

现在,在我的应用程序是从创建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();

任何人都可以帮助我,我做错了什么。

回答

0

这不会按照您希望的方式工作。 XML元素由标签名称标识,标签名称需要与Java类中的字段名称相对应。一旦<Record>已被建立为类Record的值,则<Student>在该类的字段中没有对应项,并且JAXB无法解组此内容。

你应该能够解组8after这个修改后的类改变<Student><student>):

class Record{ 
    private Student student; 
    public Student getStudent(){ 
     return student; 
    } 
    public void setStudent(Student value){ 
     student = value; 
    } 
} 

(请注意,还有另一个问题是由于类型不匹配的字符串与人的类人场记录)。