java 什么是文本节点值的 Jaxb 等价物?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1496052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the Jaxb equivalent of a Text node value?
提问by ra9r
I am looking to convert a class that looks like this ...
我正在寻找转换一个看起来像这样的类......
public class Amenity {
public String id;
public String value;
}
into the following XML using JaxB annotations:
使用 JaxB 注释转换为以下 XML:
<amenity id="id-string-here">value-string-here</amenity>
Does anyone know what annotation to use on the valuemember variable to accomplish this? The closest I've gotten so far is:
有谁知道在value成员变量上使用什么注释来完成这个?到目前为止我得到的最接近的是:
@XmlRootElement
public class Amenity {
@XmlAttribute
public String id;
@XmlElement
public String value;
}
Unfortunately this approach doesn't allow me to specify that the valuemember variable should not be rendered as its own tag <value></value>.
不幸的是,这种方法不允许我指定value不应将成员变量呈现为它自己的 tag <value></value>。
回答by jarnbjo
I'm not 100% sure about this, but try to use an @XmlValueannotation instead of @XmlElement.
我对此不是 100% 确定,但尝试使用@XmlValue注释而不是@XmlElement.
回答by bdoughan
It looks like the question was referring to text nodes not CDATA nodes, but here is a link on how EclipseLink JAXB (MOXy) handles CDATA:
看起来问题是指文本节点而不是 CDATA 节点,但这里有一个关于 EclipseLink JAXB (MOXy) 如何处理 CDATA 的链接:
回答by Bozho
This documentationwrites:
该文档写道:
Q. How can I cause the Marshaller to generate CDATA blocks?
A. This functionality is not available from JAXB directly, but you can configure an Apache Xerces-J XMLSerializer to produce CDATA blocks. Please review the JaxbCDATASample.java sample app for more detail.
问:如何让 Marshaller 生成 CDATA 块?
A. 此功能不能直接从 JAXB 获得,但您可以配置 Apache Xerces-J XMLSerializer 来生成 CDATA 块。有关更多详细信息,请查看 JaxbCDATASample.java 示例应用程序。
(btw, this does not answer your particular question, but since the question title is misleading, and this is the first google result for jaxb CDATA, I'm answering a bit different question)
(顺便说一句,这并没有回答您的特定问题,但由于问题标题具有误导性,而且这是 的第一个谷歌结果jaxb CDATA,我正在回答一个有点不同的问题)
回答by Wez
JAXB does not support marshaling/marshaling to/from CDATA xml types.
JAXB 不支持对 CDATA xml 类型进行编组/编组。

