我必须取消/编组以下代码段
<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 :(得分: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
和ProtectedText
的混合