Jaxb - 解析带有值的混合XML元素

3

I have the follwoing xml element:

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

如何使用JAXB注释此属性:
protected List<Object> compOrValue;

拥有一组 COMP XML 元素和字符串值的列表,使用 JAXB 是否可能?谢谢。
1个回答

3
你可以使用@XmlAnyElement和@XmlMixed的组合来实现这一点:
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;
    }

}

1
我使用@XmlElementRefs({@XmlElementRef(name="COMP", type=COMP.class)})代替@XmlAnyElement。它按预期工作。谢谢。 - jagin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接