3

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

jagin
  • 33
  • 1
  • 3

1 Answers1

3

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

}
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    Instead of @XmlAnyElement I use @XmlElementRefs({@XmlElementRef(name="COMP", type=COMP.class)}). It wokrs as expected. Thnaks. – jagin Dec 02 '10 at 10:53