问题描述
我正在使用 JAXB 将我的数据序列化为 XML.类代码很简单,如下所示.我想为某些 Args 的值生成包含 CDATA 块的 XML.例如,当前代码生成此 XML:
I am using JAXB to serialize my data to XML. The class code is simple as given below. I want to produce XML that contains CDATA blocks for the value of some Args. For example, current code produces this XML:
<command>
<args>
<arg name="test_id">1234</arg>
<arg name="source"><html>EMAIL</html></arg>
</args>
</command>
我想将源"参数包装在 CDATA 中,如下所示:
I want to wrap the "source" arg in CDATA such that it looks like below:
<command>
<args>
<arg name="test_id">1234</arg>
<arg name="source"><[![CDATA[<html>EMAIL</html>]]></arg>
</args>
</command>
我如何在下面的代码中实现这一点?
How can I achieve this in the below code?
@XmlRootElement(name="command")
public class Command {
@XmlElementWrapper(name="args")
protected List<Arg> arg;
}
@XmlRootElement(name="arg")
public class Arg {
@XmlAttribute
public String name;
@XmlValue
public String value;
public Arg() {};
static Arg make(final String name, final String value) {
Arg a = new Arg();
a.name=name; a.value=value;
return a; }
}
推荐答案
注意:我是EclipseLink JAXB (MOXy) 负责人和 JAXB (JSR-222) 专家组.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
如果您使用 MOXy 作为 JAXB 提供程序,那么您可以利用 @XmlCDATA
扩展:
If you are using MOXy as your JAXB provider then you can leverage the @XmlCDATA
extension:
package blog.cdata;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlCDATA;
@XmlRootElement(name="c")
public class Customer {
private String bio;
@XmlCDATA
public void setBio(String bio) {
this.bio = bio;
}
public String getBio() {
return bio;
}
}
了解更多信息
- http://bdoughan.blogspot.com/2010/07/cdata-cdata-run-run-data-run.html
- http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
这篇关于如何使用 JAXB 生成 CDATA 块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!