2010-11-27 85 views
3

我都有以下XML元素:JAXB - umarshaling混合XML元素值

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

如何使用JAXB注释此属性:

protected List<Object> compOrValue; 

有COMP XML elemnts和字符串列表值。

JAXB可以吗?

感谢

回答

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

而不是@XmlAnyElement我用@XmlElementRefs({@ XmlElementRef将(名称= “COMP”,类型= COMP.class)})。它如预期的那样。 Thnaks。 – jagin 2010-12-02 10:53:58