Java 从XML响应中解组子元素
我正在尝试解组一个XML响应。它的结构如下Java 从XML响应中解组子元素,java,jaxb,unmarshalling,Java,Jaxb,Unmarshalling,我正在尝试解组一个XML响应。它的结构如下 <employee> <name> David </name> <age> 33 </age> <dateofjoin> <year>2012</year> <month>10</month> <day>01</day> </dateofjoin> </employee>
<employee>
<name> David </name>
<age> 33 </age>
<dateofjoin>
<year>2012</year>
<month>10</month>
<day>01</day>
</dateofjoin>
</employee>
下面是我的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;
}
}
我试图通过以下方式一次性解开这些响应
但我能得到员工的名字,年龄。对于连接元素的日期,如何在解组过程中获取元素值。我不想再进行解组。我可以看出这不是一种好的编程方式
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应该已经包含在未签名的employee对象中否。我刚刚在URL中传递了我的XML响应。我可以看到有两个类文件。Dateofjoin的属性(如日、月、年)在employee类中不可用。它们在不同的类中。它们在不同的类中并不重要。Jaxb上下文知道Dateofjoin,因为它从employee遍历到Dateofjoin,所以解组器知道如何处理属于Dateofjoin的元素。。
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();
}