从XML响应中解组SubElements

时间:2015-02-25 14:48:23

标签: java jaxb unmarshalling

我正在尝试解组XML响应。它在以下结构中

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

以下是我的员工类

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

以下是我的dateofjoin类

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

我试图通过以下方式一次性解除这些响应 但我能得到员工的姓名,年龄。对于join元素的日期,如何在unmarshall进程中获取元素值。我不想再次进行解组。我可以看到它不是一种好的编程方式

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();

        }

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

1 个答案:

答案 0 :(得分:3)

dataofjoin应该已经包含在unmarshalled员工对象