我面临着一个很大的问题,因为即使我正确使用JAXB API来将xml字符串映射到java对象,我也被卡住了。 为了节省时间,我不会写真正的字段,而是写XML的一般模式:
<A>
<B>
<B1></B1>
<B2></B2>
<B3></B3>
<B4></B4>
<C>
<D>
<D1></D1>
<D2></D2>
</D>
</C>
<B5></B5>
<B6></B6>
</B>
<E>
<F>
<F1></F1>
<F2></F2>
<F3></F3>
</F>
</E>
<G>
<G1></G1>
<G2></G2>
<G3></G3>
</G>
</A>
所以在这种情况下,我的A类构建为
@XmlRootElement(name = "A")
@XmlAccessorType(XmlAccessType.FIELD)
public class A{
@XmlElement(name = "B")
private B objB;
@XmlElement(name = "C")
private String objC= "";
@XmlElement(name = "E")
private String objE= "";
@XmlElement(name = "F")
private F objF;
@XmlElement(name = "D")
private D objD;
@XmlElement(name = "G")
private G objG;
因此,包装器类A具有与XML的适当部分有关的对象的属性。因此,每个字母都有自己的类,该类代表我项目中的一个对象。 使用这个:
JAXBContext jaxbContext = JAXBContext.newInstance(A.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader("xml string here");
A element= (A) unmarshaller.unmarshal(reader);
我无法在自定义嵌套对象中正确映射我的xml字符串。因此,我的目标是将整个xml字符串映射到A类中,这将自动映射其属性,该属性也是字符串的xml部分。我还为每个子类中的每个属性声明了注释@Xmlelement。最后,E和C没有适当的类,因为它们只是分别作为D类和F类的分隔符,因此它们在主类A中被简单地声明为Strings。
我从未将JAXB用于如此复杂的数据模型,所以我不知道该如何进行。非常感谢愿意回答的人。
答案 0 :(得分:0)
import javax.xml.bind.annotation.XmlElement; 导入javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name =“ A”) 公共类A {
private B b;
private E e;
private G g;
@XmlElement(name = "B")
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
@XmlElement(name = "E")
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
@XmlElement(name = "G")
public G getG() {
return g;
}
public void setG(G g) {
this.g = g;
}
}
导入javax.xml.bind.annotation.XmlElement;
公共B类{
private String B1;
private String B2;
private String B3;
private String B4;
private C c;
private D d;
private String B5;
private String B6;
@XmlElement(name = "B1")
public String getB1() {
return B1;
}
public void setB1(String b1) {
B1 = b1;
}
@XmlElement(name = "B2")
public String getB2() {
return B2;
}
public void setB2(String b2) {
B2 = b2;
}
@XmlElement(name = "B3")
public String getB3() {
return B3;
}
public void setB3(String b3) {
B3 = b3;
}
@XmlElement(name = "B4")
public String getB4() {
return B4;
}
public void setB4(String b4) {
B4 = b4;
}
@XmlElement(name = "C")
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
@XmlElement(name = "B5")
public String getB5() {
return B5;
}
public void setB5(String b5) {
B5 = b5;
}
@XmlElement(name = "B6")
public String getB6() {
return B6;
}
public void setB6(String b6) {
B6 = b6;
}
}
import javax.xml.bind.annotation.XmlElement; 导入javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name =“ C”) 公共类C {
private D d;
@XmlElement(name = "D")
public D getD() {
return d;
}
public void setD(D d) {
this.d = d;
}
}
import javax.xml.bind.annotation.XmlElement; 导入javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name =“ D”) 公共类D {
private String D1;
private String D2;
@XmlElement(name = "D1")
public String getD1() {
return D1;
}
public void setD1(String d1) {
D1 = d1;
}
@XmlElement(name = "D2")
public String getD2() {
return D2;
}
public void setD2(String d2) {
D2 = d2;
}
}
import javax.xml.bind.annotation.XmlElement; 导入javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name =“ E”) 公共类E {
private F f;
@XmlElement(name = "F")
public F getF() {
return f;
}
public void setF(F f) {
this.f = f;
}
}
导入javax.xml.bind.annotation.XmlElement;
公共类F {
private String F1;
private String F2;
private String F3;
@XmlElement(name = "F1")
public String getF1() {
return F1;
}
public void setF1(String f1) {
F1 = f1;
}
@XmlElement(name = "F2")
public String getF2() {
return F2;
}
public void setF2(String f2) {
F2 = f2;
}
@XmlElement(name = "F3")
public String getF3() {
return F3;
}
public void setF3(String f3) {
F3 = f3;
}
}
导入javax.xml.bind.annotation.XmlElement;
公共类G {
private String G1;
private String G2;
private String G3;
@XmlElement(name = "G1")
public String getG1() {
return G1;
}
public void setG1(String g1) {
G1 = g1;
}
@XmlElement(name = "G2")
public String getG2() {
return G2;
}
public void setG2(String g2) {
G2 = g2;
}
@XmlElement(name = "G3")
public String getG3() {
return G3;
}
public void setG3(String g3) {
G3 = g3;
}
}