2010-11-27 37 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