gpt4 book ai didi

java - JAXBContext 解码具有多个 rootElement 的 XML 字符串

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:36 25 4
gpt4 key购买 nike

下面是我的 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);

最佳答案

使用这些类

实体1

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

}

实体2

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

Entity3

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

}

实体3

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

关于java - JAXBContext 解码具有多个 rootElement 的 XML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33817188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com