Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add an attribute to a text element in JAXB?

Tags:

java

xml

jaxb

How can I produce the following XML fragment using JAXB?

<sequence md5="1de2cf633901ff1f00785735c8ce7e70">MPTINSALRKVARVRLTSG</sequence>

My class is as follows:

@XmlType(name = "SequenceType")
public class Sequence {

    private String md5;
    private String sequence;

    @XmlAttribute
    public String getMd5() {
        return md5;
    }

    // JAXB annotation here??
    public String getSequence() {
        return sequence;
    }

}
like image 338
Antony Quinn Avatar asked Dec 01 '10 16:12

Antony Quinn


1 Answers

@XmlValue on the sequence property.

@XmlType(name = "SequenceType")
public class Sequence {

    private String md5;
    private String sequence;

    @XmlAttribute
    public String getMd5() {
        return md5;
    }

    @XmlValue
    public String getSequence() {
        return sequence;
    }

}
like image 169
bdoughan Avatar answered Sep 23 '22 14:09

bdoughan