我必须取消/编组以下代码段

    <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注释的内容,但是没有找到映射此类内容的示例。

有任何想法吗?

最好的祝福,
帕斯卡

最佳答案

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列表包含StringProtectedText的混合

07-26 01:39