简体   繁体   中英

How to parse an XML with several attributes for each element using JAXB?

I have an xml of the below format:

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

There are 33 such "fld"s and the attributes vary widely. Below is a sample that i tried to proceed with:

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

this does not work because class Fld00 is a non static inner class.

When i change it to a static class, and add an XmlAccessType.FIELD it throws a "Class has two properties of the same name "payer"" exception. Below is the code with static class:

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

Please let me know if you find a way to proceed using JAXB.

Thanks

Below is a class that is capable of handling the XML as you have described it. It would be simpler (to use) if you would write the classes for the fld-xx as separate classes, not static classes of what I have called RecordType.

Note that there is a subtle difference between the way these static classes are written: the annotation @XmlAttribute is attached to the field and not to the method.

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

}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM