我必须取消/编码以下代码段
<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 的内容,但尚未找到映射此类内容的示例。
有什么想法吗?
最诚挚的问候, 帕斯卡
最佳答案
为 protectedText
元素创建新类:
@XmlRootElement(name = "protectedText")
class ProtectedText implements Serializable{
@XmlValue
public String value;
}
现在更改 AcrossParagraph
中的 content
属性,如下所示:
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
的混合
关于java - 文本内容中的 JAXB Moxy 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31381348/