我似乎无法让@XmlCData注释正常工作,即使正确设置了MOXy。
我的代码附加的输出:
<Employee>
<id>1</id>
<MOXy>
<is>
<working>
<name>Bill</name>
</working>
</is>
</MOXy>
<notes>
<<html>
<p>Bill likes to eat quite loudly at his desk.</p>
</html>
</notes>
</Employee>它应该将notes元素的内容作为CDATA输出。
我正在将它部署到VMWare Fabric v2.9中,以确定它的价值。
发布于 2014-06-10 20:01:15
为我工作。
你用来测试什么?使用web浏览器测试web服务将删除CDATA元素,除非您查看源代码。
发布于 2014-06-10 19:20:20
我无法重现你所看到的错误。下面是我想象的你们班的样子,和你们的相比怎么样?
Java模型
雇员
下面是一个基于您的问题的示例类:
进口javax.xml.bind.annotation.;进口org.eclipse.persistence.oxm.annotations.
@XmlRootElement(name="Employee")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
int id;
@XmlPath("MOXy/is/working/name/text()")
String name;
@XmlCDATA
String notes;
}jaxb.properties
要使用MOXy作为您的JAXB提供程序,您需要在与您的域模型相同的包中包含一个名为jaxb.properties的文件,其中包含以下条目(参见:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory演示代码
Demo
下面是一些演示代码,您可以运行,以确保一切正常运行。
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Employee.class);
Employee employee = new Employee();
employee.id = 1;
employee.name = "Bill";
employee.notes = "<html><p>Bill likes to eat quite loudly at his desk.</p></html>";
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(employee, System.out);
}
}输出
下面是运行演示代码获得的输出。
<?xml version="1.0" encoding="UTF-8"?>
<Employee>
<id>1</id>
<MOXy>
<is>
<working>
<name>Bill</name>
</working>
</is>
</MOXy>
<notes><![CDATA[<html><p>Bill likes to eat quite loudly at his desk.</p></html>]]></notes>
</Employee>https://stackoverflow.com/questions/24148848
复制相似问题