下面是我的XML字符串,我在访问Entity3列表时遇到了问题。
<Entity1>
<field1>Value1</field1>
<field2>Value2</field2>
<Entity2>
<field3>Value1</field3>
<field4>Value2</field4>
</Entity2>
<Entity3s>
<Entity3List>
<Entity3>
<field5>Value1</field5>
<field6>Value2</field6>
</Entity3>
<Entity3>
<field5>Value1</field5>
<field6>Value2</field6>
</Entity3>
</Entity3List>
</Entity3s>
</Entity1>
下面是我的java对象类
实体
@XmlRootElement(name= "Entity")
public class Entity {
private String field1;
private String field2;
private String field3;
private String field4;
private List<Entity3> entity3s;
}
实体2
public class Entity2 {
private String field3;
private String field4;
}
实体3
public class Entity3 {
private String field5;
private String field6;
}
我应该如何注释Entity3列表,因为它包含两个根元素<Entity3s>
和<Entity3List>
jaxbContext = JAXBContext.newInstance(Entity1.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Entity1 entity1= (Entity1) unmarshaller.unmarshal(sr);
答案 0 :(得分:0)
使用这些类
<强> ENTITY1 强>
@XmlRootElement(name= "Entity1")
public class Entity {
private String field1;
private String field2;
private Entity2 entity2;
private Entity3s entity3s;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
@XmlElement(name = "Entity2")
public Entity2 getEntity2() {
return entity2;
}
public void setEntity2(Entity2 entity2) {
this.entity2 = entity2;
}
@XmlElement(name = "Entity3s")
public Entity3s getEntity3s() {
return entity3s;
}
public void setEntity3s(Entity3s entity3s) {
this.entity3s = entity3s;
}
}
<强> ENTITY2 强>
@XmlRootElement
public class Entity2 {
private String field3;
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
public String getField4() {
return field4;
}
public void setField4(String field4) {
this.field4 = field4;
}
private String field4;
}
<强> Entity3s 强>
public class Entity3s {
private Entity3List entity3List;
@XmlElement(name = "Entity3List")
public Entity3List getEntity3List() {
return entity3List;
}
public void setEntity3List(Entity3List entity3List) {
this.entity3List = entity3List;
}
}
<强> Entity3List 强>
public class Entity3List {
private List<Entity3> entity3s;
@XmlElement(name = "Entity3")
public List<Entity3> getEntity3s() {
return entity3s;
}
public void setEntity3s(List<Entity3> entity3s) {
this.entity3s = entity3s;
}
}
<强> ENTITY3 强>
public class Entity3 {
private String field5;
private String field6;
public String getField5() {
return field5;
}
public void setField5(String field5) {
this.field5 = field5;
}
public String getField6() {
return field6;
}
public void setField6(String field6) {
this.field6 = field6;
}
}