Jaxb - umarshaling mixed xml element with value

2019-01-28 06:47发布

问题:

I have the follwoing xml element:

<FIELD1><COMP VAR="A">text B</COMP> inner text <COMP VAR="B">text B</COMP></FIELD1>

How to annotate this property with JAXB:

protected List<Object> compOrValue;

to have a list of COMP xml elemnts and String values.

Is it possible with JAXB?

Thanks

回答1:

You can use a combination of @XmlAnyElement and @XmlMixed to achieve this:

import java.util.List;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="FIELD1")
public class Root {

    protected List<Object> compOrValue;

    @XmlAnyElement
    @XmlMixed
    public List<Object> getCompOrValue() {
        return compOrValue;
    }

    public void setCompOrValue(List<Object> compOrValue) {
        this.compOrValue = compOrValue;
    }

}


标签: jaxb