我似乎无法让@XmlCData 注释起作用,即使 MOXy 已正确设置。
我的代码,attached , 输出:
<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,因为它物有所值。
最佳答案
我无法重现您看到的错误。以下是我设想的您类(class)的样子,与您的类(class)相比如何?
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
演示代码
演示
下面是一些演示代码,您可以运行它来查看一切是否正常。
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>
关于java - MOXy XmlCDATA 注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24148848/