如何使用JAXB生成CDATA块? [英] How to generate CDATA block using JAXB?
问题描述
我正在使用JAXB将我的数据序列化为XML。类代码很简单,如下所示。我想生成包含CDATA块的XML,用于某些Args的值。例如,当前代码生成此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中包装sourcearg,使其如下所示:
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
extension:
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 / specify-eclipselink-moxy-as-your.html
- 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块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!