Search code examples
javamarshallingjaxb2unmarshalling

JAXB2 annotations: troubles rendering a value in a list


Ihave some issues trying to render the following xml code with JAXB2 annotations:

....
<bizTransactionList>
    <bizTransaction type="urn:epcglobal:fmcg:btt:po">
        http://transaction.acme.com/po/54847
    </bizTransaction>
    <!--other entries -->
</bizTransactionList>
...

following the documentation I managed to solve the problem as follows: 1) inside the class which owns the list I annotate the list getter this way

@XmlElementWrapper(name="bizTransactionList")
@XmlElement(name="bizTransaction")
public List<BizTransaction> getBizTransactionList() {
    return bizTransactionList;
}

2) I create the BizTransaction class as follows

public class BizTransaction {

    private URI transactionId;
    private URI type;

    @XmlValue
    public URI getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(URI transactionId) {
        this.transactionId = transactionId;
    }

    @XmlAttribute
    public URI getType() {
        return type;
    }

    public void setType(URI type) {
        this.type = type;
    }

    public BizTransaction() {
        // TODO Auto-generated constructor stub
    }

    public BizTransaction(URI transactionId, URI type) {
        super();
        this.transactionId = transactionId;
        this.type = type;
    }

}

now the problem is that the attribute works fine, but the value (i.e. the transactionId variable) is always null. Can you help me to spot the problem?


Solution

  • That's a reasonable model but your example XML is not valid because the leading and trailing white space around the bizTransaction element value means it is not a valid URI. If you said

        <bizTransaction type="urn:foo">http://transaction.acme.com/po/54847</bizTransaction>
    

    then it would work as you expect.