cuttcards
cuttcards

Reputation: 278

MOXy XmlCDATA annotation does not work

I cannot seem to get the @XmlCData annotation to work, even though MOXy is correctly set up.

My code, attached, outputs:

<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>

it should be outputting the content of the notes element as CDATA.

I am deploying this to VMWare Fabric v2.9, for what it's worth.

Upvotes: 1

Views: 1993

Answers (2)

cuttcards
cuttcards

Reputation: 278

Works for me.

What are you using to test? Using a web browser for testing web services will strip out the CDATA element, unless you View Source.

Upvotes: 1

bdoughan
bdoughan

Reputation: 149037

I haven't been able to reproduce the error that you are seeing. Below is what I envision your class looking like, how does it compare to yours?

Java Model

Employee

Below is a sample class based on your question:

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

To use MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo Code

Demo

Below is some demo code you can run to see that everything works.

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);
    }

}

Output

Below is the output you get from running the demo code.

<?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>

Upvotes: 2

Related Questions