从 XML 响应中解组子元素

Unmarshalling SubElements from XML Response

我正在尝试解组 XML 响应。结构如下

<employee>
 <name> David </name>
 <age> 33 </age>
 <dateofjoin>
 <year>2012</year>
 <month>10</month>
 <day>01</day>
 </dateofjoin>
</employee>

以下是我的员工Class

public class Employee
{
    private Dateofjoin dateofjoin;

    private String name;

    private String age;

    public Dateofjoin getDateofjoin ()
    {
        return dateofjoin;
    }

    public void setDateofjoin (Dateofjoin dateofjoin)
    {
        this.dateofjoin = dateofjoin;
    }

    public String getName ()
    {
        return name;
    }

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

    public String getAge ()
    {
        return age;
    }

    public void setAge (String age)
    {
        this.age = age;
    }
}

以下是我的加入日期class

    public class Dateofjoin
{
    private String month;

    private String year;

    private String day;

    public String getMonth ()
    {
        return month;
    }

    public void setMonth (String month)
    {
        this.month = month;
    }

    public String getYear ()
    {
        return year;
    }

    public void setYear (String year)
    {
        this.year = year;
    }

    public String getDay ()
    {
        return day;
    }

    public void setDay (String day)
    {
        this.day = day;
    }
}

我正在尝试通过以下方式一次解组这些响应 但我能够得到员工姓名、年龄。对于加入元素的日期,我如何在解组过程中获取元素值。我不想进行解组 again.I 可以看出这不是一种好的编程方式

while(xsr1.hasNext()) {
            if(xsr1.isStartElement() && xsr1.getLocalName().equals("employee")) {
                JAXBContext jc = JAXBContext.newInstance(Employee.class);
                Unmarshaller unmarshaller = jc.createUnmarshaller();
                Employee jb = unmarshaller.unmarshal(xsr1,Employee.class).getValue();
                System.out.println("Employee" + jb.getName()+"Age.getAge());
        }


            xsr1.next();

        }

非常感谢任何帮助。也希望看到一些解释,以便我可以根据问题清楚地理解

dataofjoin 应该已经包含在未编组的员工对象中