javaxmljackson-dataformat-xml

How to use Jackson to deserialse an XML element with both value and attribute


I have been struggling to find a way to use Jackson to de-serialise this XML into Java:

<bitstream encoding="base64Binary">ew0KIklucH...bkpBTVMgQUEiDQp9</bitstream>

I have searched for ages and I can't find a solution to what seems a common XML structure: an element with both a value and attribute. Can anyone help me?

Here is my current code:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Bitstream {
    @JacksonXmlProperty(isAttribute = true)
    private String encoding;
    @JacksonXmlProperty(localName = "bitstream")
    private String bitstrm;

    public String getEncoding() {
        return encoding;
    }
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }
    public String getBitstream() {
        return bitstrm;
    }
    public void setData(String bitstrm) {
        this.bitstrm = bitstrm;
    }

This sets the encoding attribue, but bitstrm is null. Do I need to create a custom de-serialiser... or just have a "Duh!" moment when I see the answer?


Solution

  • Try using @JacksonXmlText instead of @JacksonXmlProperty for the bitstrm field:

    @JacksonXmlText
    private String bitstrm;