我有一个以下格式的xml:
<fld-03 m="01" d="01" y="1965" sex-m="" sex-f="1"></fld-03>
<fld-04 last="lastname" first="firstname" middle=""></fld-04>
<fld-05 addr="Richardson" city="Dallas" state="TX" zip="75080" phone=""></fld-05>
<fld-06 self="" spouse="1" child="" other=""></fld-06>
<fld-07 addr="" city="" state="" zip="" phone=""></fld-07>
有33个这样的“fld”,属性差异很大。 以下是我尝试继续的示例:
@XmlRootElement(name = "myroot")
class Formxml implements Serializable{
@XmlElement (name="fld-00")
private Fld00 fld00 = new Fld00();
public class Fld00 {
private String payer;
@XmlAttribute(name = "payer", required = true)
String getPayer() {
return payer
}
void setPayer(String payer) {
this.payer = payer
}
}
}
这不起作用,因为类Fld00
是非静态内部类。
当我将其更改为静态类并添加XmlAccessType.FIELD
时,它会引发"Class has two properties of the same name "payer""
异常。
下面是带有静态类的代码:
@XmlRootElement(name = "Claims")
class Formxml implements Serializable{
@XmlElement (name="fld-00")
private Fld00 fld00 = new Fld00();
@XmlAccessorType(XmlAccessType.FIELD)
public static class Fld00 {
private String payer;
@XmlAttribute(name = "payer", required = true)
String getPayer() {
return payer
}
void setPayer(String payer) {
this.payer = payer
}
}
}
如果您找到使用JAXB的方法,请告诉我。
由于
答案 0 :(得分:0)
下面是一个能够按照您的描述处理XML的类。如果你将fld-xx的类作为单独的类编写,而不是我称之为RecordType的静态类,那么它将更简单(使用)。
请注意,这些静态类的编写方式之间存在细微差别:注释@XmlAttribute
附加到字段而不是方法。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "RecordType", propOrder = {
"fld03",
"fld04"
})
public class RecordType {
@XmlElement(name = "fld-03", required = true)
protected RecordType.Fld03 fld03;
@XmlElement(name = "fld-04", required = true)
protected RecordType.Fld04 fld04;
public RecordType.Fld03 getFld03() { return fld03; }
public void setFld03(RecordType.Fld03 value) {
this.fld03 = value;
}
public RecordType.Fld04 getFld04() { return fld04; }
public void setFld04(RecordType.Fld04 value) {
this.fld04 = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class Fld03 {
@XmlAttribute(name = "m", required = true)
protected int m;
public int getM() { return m; }
public void setM(int value) { this.m = value; }
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class Fld04 {
@XmlAttribute(name = "lastname", required = true)
protected String lastname;
public String getLastname() { return lastname; }
public void setLastname(String value) { this.lastname = value; }
}
}