Nazariy
Nazariy

Reputation: 727

How to use inheritance when reading from XML files using JAXB

Hi I have these classes right here: As you can see Faculty and Student inherits name from Persons. And faculty and student all have their unique attributes with that as well

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

Provided that I have these XML files:

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

and the Faculty XML file:

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

how would I set up the classes for unmarshalling from the xml files. I have done this with normal xml files where there is not inheritance, but I was just wondering how would I do this without writing the same attributes in two classes. Thank You!

Upvotes: 2

Views: 335

Answers (1)

icza
icza

Reputation: 418435

Inheritence doesn't complicate anything when marshaling or unmarshalling using JAXB.

One thing to note here: in your Java classes you have fields named studentId and facultyId (with capital I letters) but in XML these elements appear with small i letters: <studentid> and <facultyid>. Either you make those written the same, or you have to tell JAXB what will be the XML element names storing the values of these fields with annotation like this:

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
}

Now that we told how the id fields are represented in XML files, we have to create wrapper classes to read a collection of students or a collection of faculties:

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

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

Now you have all you need. You can unmarshal the XML files like this:

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

Upvotes: 1

Related Questions