2014-10-26 78 views
2

嗨我在這裏有這些類: 正如您所看到的,Faculty和Student從Persons繼承名稱。 而教師和學生都有其獨特的屬性與和如何在使用JAXB從XML文件中讀取時使用繼承

public class Persons{ 
    protected String name; 

    public Persons(){ 
     this(null); 
    } 
    public Persons(String name){ 
     this.name = name; 
    } 

    public String getName(){ 
     return this.name; 
    } 
    public String setName(String name){ 
     this.name = name; 
    } 
} 

public class Student extends Persons{ 
    private String studentId; 

    public Student(){ 
     this(null,null); 
    } 
    public Student(String name,String studentId){ 
     this.name = name; 
     this.studentId = studentId; 
    } 



} 
public class Faculty extends Persons{ 
    private String facultyId; 

    public Faculty(){ 
     this(null,null); 
    } 
    public Faculty(String name,String facultyId){ 
     this.name = name; 
     this.facultyId = facultyId; 
    } 
} 

只要我有這些XML文件:

<persons> 
    <student> 
     <name>Example 1</name> 
     <studentid>id</studentid> 
    </student> 
    <student> 
     <name>Example 1</name> 
     <studentid>id</studentid> 
    </student> 
</persons> 

和教師XML文件:

<persons> 
    <faculty> 
     <name>Example 1</name> 
     <facultyid>id</facultyid> 
    </faculty> 
    <faculty> 
     <name>Example 1</name> 
     <facultyid>id</facultyid> 
    </faculty> 
</persons> 

如何我是否會設置從xml文件解組的類。我用普通的xml文件做了這些,但沒有繼承,但我只是想知道如何在不寫兩個類中的相同屬性的情況下做到這一點。謝謝!

+0

您可能會嘗試搜索一下。 http://stackoverflow.com/questions/16429717/jaxb-and-inheritance – 2014-10-26 20:13:31

+0

我一直在尋找。我只是很難理解我想如何保存一份學生名單。我會在Person類還是Students類中設置該屬性?我知道你通常會這樣做:List 學生 – Nazariy1995 2014-10-26 20:51:43

+0

@ Brenden1995沒有時間做完整的答案 - 但是你可以在模式方面用替代組和在JAXB方用'@XmlElementRef '。同時檢查'xsi:type'。 – lexicore 2014-10-27 13:34:20

回答

1

當使用JAXB進行編組或解組時,繼承不會使任何事情複雜化。

有一點要注意這裏:在Java類中有一個名爲studentIdfacultyId(用大寫字母我),但在XML這些元素出現小我信領域:<studentid><facultyid>。要麼你讓那些寫的一樣,或者你要告訴JAXB會有怎樣的XML元素名稱存儲這些字段的值與註釋是這樣的:

public class Student extends Persons { 
    @XmlElement(name = "studentid") 
    private String studentId; 

    // ... rest of your class 
} 

public class Faculty extends Persons { 
    @XmlElement(name = "facultyid") 
    private String facultyId; 

    // ... rest of your class 
} 

現在我們告訴ID字段在代表XML文件,我們必須創建包裝類閱讀學生的集合或院系的集合

public class Students { 
    @XmlElement(name = "student") 
    public List<Student> students; 
} 

public class Faculties { 
    @XmlElement(name = "faculty") 
    public List<Faculty> faculties; 
} 

現在,你有你所需要的。您可以像這樣解組XML文件:

Students students = JAXB.unmarshal(new File("students.xml"), Students.class); 
Faculties faculties = JAXB.unmarshal(new File("faculties.xml"), Faculties.class); 
+0

您可以在'Student' /'Person'和'Students' /'Persons'中定義_different_屬性,這不是很有趣(不是太多的繼承)。一個有趣的部分是在Person中只有一個'id'屬性,或者在Persons中只有一個集合屬性('people',或其他)被超類映射。即'學生'中的'學生身份證'或'教員身份證'中的'facultyId'。 – lexicore 2014-10-28 08:35:29

+0

@lexicore不,我沒有定義屬性,提問者定義了它們。我只是在使用他/她提供的課程。 name字段是層次結構中的常用字段,自定義id字段是suclasses中的附加字段。你的評論應該發佈在問題本身上。 – icza 2014-10-28 10:16:17

+0

你是對的,這不是這個問題的一部分。 – lexicore 2014-10-28 10:59:18