本文介绍了JAXB-具有XML字符串字段的元组Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个具有String属性的对象,该对象具有XML字符串.像:
class myObject {
String xml;
@XmlElement(name = "xml", type = String.class)
public String getXml() {
return xml;
}
public void setXml(String xml) {
this.xml = xml;
}
}
我为此属性设置了XML字符串-例如
i set an XML String to this property - such as
myObject.setXml("<xml>bbb</xml>");
现在我想使用JAXB封送它,然后我得到:
now i want to marshal it using JAXB and i get:
<xml><xml>bbb</xml></xml>
我想去的地方
<xml>bbb</xml>
我该怎么办?
问题是String xml将格式正确的XML作为字符串存储.因此我希望在不转义XML字符的情况下封送该字符串.
the problem is that String xml, stores a well formatted XML as a string. so I want this string to be marshaled without escaping the XML characters.
推荐答案
如果要在Java模型中将XML片段存储为String
,则可以将@XmlAnyElement
注释与指定的DomHandler
一起使用为此.
If you want to store an XML fragment as a String
in your Java model, then you can use the @XmlAnyElement
annotation with a DomHandler
specified to achieve this.
堆栈溢出示例
- JAXB use String as it is
- How to marshall a string using JAXB that sometimes contains XML content and sometimes does not?
- Using JAXB to extract inner text of XML element
这篇关于JAXB-具有XML字符串字段的元组Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!