MOXy XmlCDATA注释不起作用

时间:2014-06-10 18:57:13

标签: java jaxb eclipselink moxy

即使正确设置了MOXy,我似乎也无法使@XmlCData注释生效。

我的代码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,以实现它的价值。

2 个答案:

答案 0 :(得分:3)

我无法重现您所看到的错误。以下是我想象你的课程,你的课程与你的相比如何?

Java模型

<强>员工

以下是基于您的问题的示例类:

import javax.xml.bind.annotation。; import 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>

答案 1 :(得分:1)

适合我。

你在测试什么?使用Web浏览器测试Web服务将删除CDATA元素,除非您查看源。