1 回答

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊
你可以試試這個(gè):
使用如下參考類,
@XmlAccessorType(XmlAccessType.FIELD)
public class Reference {
? ? @XmlAttribute
? ? private String id;
}
還有你的 Root 類,
@XmlRootElement(name="Date")
public class TestPojo {
? ? @XmlMixed
? ? @XmlAnyElement
? ? private List<Object> textContent;
? ? @XmlElement
? ? private Reference reference;
}
這將解組給您參考元素和列表中的其他所有內(nèi)容。
對(duì)于您的示例,它將有 2 個(gè)條目。日期值/文本以及制表符 (\t) 和換行符 (\n),以及另一個(gè)帶有換行符的條目。
所以你可以使用這個(gè)列表來處理內(nèi)容并使用你想要的。
如果有更清潔的解決方案,我很感興趣。干杯
更新回復(fù)評(píng)論:
為了更清楚地修復(fù)。我所做的是使用@XmlElement而不是@XmlElementRef單個(gè)引用而不是列表(因?yàn)檫@就是我在 xml 中看到的)。
我還添加了@XmlAnyElement混合內(nèi)容的注釋,使其成為一個(gè)列表。這就是修復(fù)它的原因。因此,堅(jiān)持你的課程,它看起來像下面這樣:
@XmlRootElement(name="Date")
public class TestPojo {
? ? List<Object> textContent;
? ? Reference ref;
? ? @XmlMixed
? ? @XmlAnyElement
? ? public List<Object> getTextContent() {
? ? ? ? return textContent;
? ? }
? ? public void setTextContent(List<Object> textContent) {
? ? ? ? this.textContent = textContent;
? ? }
? ? @XmlElement(name="reference")
? ? public Reference getRef() {
? ? ? ? return ref;
? ? }
? ? public void setRef(Reference ref) {
? ? ? ? this.ref = ref;
? ? }
}
這@XmlAccessorType
節(jié)省了我編寫 getter 和 setter 的時(shí)間。
添加回答
舉報(bào)