2015-07-13 53 views
0

我有這不同於/馬歇爾下面的代碼片段文本內容裏面JAXB莫西元

<para sizeInfoId="sizeInfo2" styleId="mono">Franz jagt im komplett verwahrlosten <protectedText>Taxi</protectedText> quer durch Bayern.</para> 

我的Java模型看起來像這樣

import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlTransient; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlValue; 

@XmlType(propOrder = { AcrossParagraph.XML_ID, AcrossParagraph.XML_STYLE_ID, AcrossParagraph.XML_SIZEINFO_ID, AcrossParagraph.XML_COMMENT, AcrossParagraph.XML_CONTENT }) 
@XmlRootElement(name = AcrossParagraph.XML_ROOT_TAG) 
public class AcrossParagraph { 

    public static final String XML_ROOT_TAG = "para"; 
    public static final String XML_ID = "id"; 
    public static final String XML_STYLE_ID = "styleId"; 
    public static final String XML_SIZEINFO_ID = "sizeInfoId"; 
    public static final String XML_COMMENT = "comment"; 
    public static final String XML_CONTENT = "content"; 

    private String id; 
    private String styleId; 
    private String sizeInfoId; 
    private String comment; 
    private String content; 

    @XmlAttribute(name = AcrossParagraph.XML_ID) 
    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    @XmlAttribute(name = AcrossParagraph.XML_STYLE_ID) 
    public String getStyleId() { 
     return styleId; 
    } 

    public void setStyleId(String styleId) { 
     this.styleId = styleId; 
    } 

    @XmlTransient 
    public void setStyleId(AcrossStyle style) { 
     this.styleId = style.getId(); 
    } 

    @XmlAttribute(name = AcrossParagraph.XML_SIZEINFO_ID) 
    public String getSizeInfoId() { 
     return sizeInfoId; 
    } 

    public void setSizeInfoId(String sizeInfoId) { 
     this.sizeInfoId = sizeInfoId; 
    } 

    @XmlTransient 
    public void setSizeInfoId(AcrossSize size) { 
     this.sizeInfoId = size.getId(); 
    } 

    @XmlAttribute(name = AcrossParagraph.XML_COMMENT) 
    public String getComment() { 
     return comment; 
    } 

    public void setComment(String comment) { 
     this.comment = comment; 
    } 

    @XmlValue 
    public String getContent() { 
     return content; 
    } 

    public void setContent(String content) { 
     this.content = content; 
    } 

} 

沒有內protectedText標籤一切正常,但我不知道如何映射內部元素。

我已經閱讀了一些關於XmlAnyElement Annotation的內容,但沒有找到像這樣映射的例子。

任何想法?

最好的問候, 帕斯卡爾

+0

[JAXB - Java/XMLValue&XMLElement衝突]的可能重複(http://stackoverflow.com/questions/15495156/jaxb-java-xmlvalue-xmlelement-conflict) – Xstian

回答

1

protectedText元素創建新類:

@XmlRootElement(name = "protectedText") 
class ProtectedText implements Serializable{ 
    @XmlValue 
    public String value; 
} 

改變現在content財產AcrossParagraph如下:

private List<Serializable> content; 

@XmlElementRef(name = "protectedText", type = ProtectedText.class) 
@XmlMixed 
public List<Serializable> getContent(){ 
    return content; 
} 

public void setContent(List<Serializable> content){ 
    this.content = content; 
} 

當你解編content列表包含String和的混合

+0

非常感謝!它的工作就像一個魅力:) – Pascal