JAXB: Marshalling and Unmarshalling CDATA block using EclipseLink MOXy

11 November 2012 By Nithya Vasudevan 8,317 views 10 Comments
5 Flares Twitter 1 Facebook 0 Google+ 4 5 Flares ×

Project Description

  • In the example provided here, we saw how to marshal and unmarshal CDATA block using JAXB RI by creating custom adapter class.
  • In this JAXB example we will see how to marshal and unmarshal CDATA block using MOXy implementation which is available as part of Eclipselink project.
  • When using MOXy JAXB implementation there is no need to write custom adapter. Just an annotation (provided by MOXy) is sufficient to handle CDATA.

Environment Used:

  • JDK 6 (Java SE 6) or later.
  • Eclipse Indigo IDE for Java EE Developers.
  • EclipseLink 2.3.2 (Download EclipseLink from here and extract the zip file). We need this for MOXy which is an implementation of JAXB API.

New project in Eclipse

Create a new Java project in Eclipse IDE and name it as JAXBCDataMoxyImpl.

Add JAR file

  • You need to add eclipselink.jar file located in “eclipselink/jlib” in your project Build Path.
  • Right click on project -> Properties -> Java Build Path from left pane -> Libraries from right pane -> Add External JARs…

Create Bean class

Create a new Class “Book.java” in the package “com.theopentutorials.jaxb.to” and copy the following code.

package com.theopentutorials.jaxb.to;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlCDATA;

@XmlRootElement(name = "book")
public class Book {

	private String name;
	private String author;
	private String publisher;
	private String isbn;
	private String description;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public String getAuthor() {
		return author;

	public void setAuthor(String author) {
		this.author = author;

	public String getPublisher() {
		return publisher;

	public void setPublisher(String publisher) {
		this.publisher = publisher;

	public String getIsbn() {
		return isbn;

	public void setIsbn(String isbn) {
		this.isbn = isbn;

	public String getDescription() {
		return description;

	public void setDescription(String description) {
		this.description = description;

	public String toString() {
		return "Book [name=" + name + ", author=" + author + ", publisher="
				+ publisher + ", isbn=" + isbn + ", description=" + description
				+ "]";

This is a simple bean class containing JAXB annotations whose object is marshalled and unmarshalled. The ‘description’ property is annotated with @XmlCDATA which is provided by MOXy and vendor specific.

jaxb.properties file

To use MOXy as your JAXB implementation you need to add a jaxb.properties file in the bean package.
Create a new file “jaxb.properties” in the package “com.theopentutorials.jaxb.to” and copy the following entry.


Create JAXB Handler (Helper class)

Create a new Class “JAXBXMLHandler.java” in the package “com.theopentutorials.jaxb.xml” and copy the following code.

package com.theopentutorials.jaxb.xml;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import com.theopentutorials.jaxb.to.Book;

public class JAXBXMLHandler {

	public static Book unmarshal(File importFile) throws JAXBException {
		Book book = new Book();
		JAXBContext context;

		context = JAXBContext.newInstance(Book.class);
		Unmarshaller um = context.createUnmarshaller();
		book = (Book) um.unmarshal(importFile);
		return book;

	public static void marshal(Book book, Class[] classes,
			Map<String, Map<String, StreamSource>> properties, File selectedFile)
			throws IOException, JAXBException {
		JAXBContext context;
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(selectedFile));
			context = JAXBContext.newInstance(classes, properties);
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			m.marshal(book, writer);
		} finally {
			try {
			} catch (IOException io) {/* ignore */
  • This is a helper class which has methods to perform marshalling and unmarshalling. These methods are called from client code (in this case, main() method).
  • By default, the marshaller implementation of the JAXB RI tries to escape characters.
  • We use an overloaded JAXBContext.newInstance(Class) to configure ‘properties’ for this instantiation of JAXBContext. The interpretation of properties is implementation specific.

MOXy binding file

MOXy JAXB implementation allows you to enable a element to use CDATA. You could use MOXy’s binding file to indicate that the book’s ‘description’ property should use CDATA.
Create a XML file in your project and name it as “oxm.xml” and copy the following code.

<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
      <java-type name="com.theopentutorials.jaxb.to.Book">
            <xml-element java-attribute="description" cdata="true"/>

Create Java Application client (main())

Create a new Class “JAXBCDataDemo.java” in the package “com.theopentutorials.jaxb.main” and copy the following code.

package com.theopentutorials.jaxb.main;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBException;
import javax.xml.transform.stream.StreamSource;
import com.theopentutorials.jaxb.to.Book;
import com.theopentutorials.jaxb.xml.JAXBXMLHandler;

public class JAXBCDataDemo {
	public static void main(String[] args) {

		Book book = new Book();
		book.setAuthor("Kathy Sierra");
		book.setPublisher("Tata McGraw Hill");

		String desc = "<p>With hundreds of practice questions and hands-on exercises, "
				+ "<b>SCJP Sun Certified Programmer for Java 6 Study Guide</b>" +
				"covers what you need to know"
				+ "--and shows you how to prepare--" +
				"for this challenging exam. </p>";

		Map<String, StreamSource> oxm = new HashMap<String, StreamSource>(1);
		oxm.put("com.theopentutorials.jaxb.to", new StreamSource("oxm.xml"));

		Map<String, Map<String, StreamSource>> properties = 
				new HashMap<String, Map<String, StreamSource>>();
		properties.put("eclipselink-oxm-xml", oxm);

		Class[] classes = { Book.class };

		try {
			//Marshalling: Writing Java object to XML file
			JAXBXMLHandler.marshal(book, classes, properties, new File("book.xml"));

			//Unmarshalling: Converting XML content to Java objects		
			Book book2 = JAXBXMLHandler.unmarshal(new File("book.xml"));
			System.out.println("Unmarshal: " + book2);
		} catch (IOException e) {
		} catch (JAXBException e) {
  • This class creates book object with ‘description’ property containing HTML tags and calls marshal method from JAXBXMLHandler helper class passing book object and the file to write the object.
  • We call unmarshal method passing the marshalled file name which returns a book object.
  • Finally, printing the book object (which calls toString() from Book class).


Run the JAXBCDataDemo.java.

Book [name=SCJP, author=Kathy Sierra, publisher=Tata McGraw Hill, isbn=856-545456736, description=
<p>With hundreds of practice questions and hands-on exercises, <b>SCJP Sun Certified Programmer for Java 6 Study Guide</b> covers what you need to know--and shows you how to prepare--for this challenging exam. </p>]

Refresh your project in Project Explorer (press F5 on your project) to see the generated XML file.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <author>Kathy Sierra</author>
    <publisher>Tata McGraw Hill</publisher>
    <description><![CDATA[<p>With hundreds of practice questions 
        and hands-on exercises, <b>SCJP Sun Certified Programmer 
        for Java 6 Study Guide</b> covers what you need to know--
        and shows you how to prepare --for this challenging exam. </p>]]>

Folder Structure

The complete folder structure of this example is shown below.

Tags: , , , , ,

  • Xor

    Nice tutorial!
    Except for 2 things; first, there is a small syntax error, in 3.4 the classname “JaxbXMLHandler” needs to be JAXBXMLHandler. Well that’s no dealbreaker.

    Secondly, I get a “java.lang.ClassCastException: javax.xml.transform.stream.StreamSource cannot be cast to java.lang.String”

    I don’t really know what’s going on and why I should create a seperate xml file with some sort of instructions for processing CData in an xml. Let alone, why putting a field of cdata in an xml file turns out to be such an amazingly complicated thing to do. Anyway, I don’t know how to fix this.

    • http://theopentutorials.com Praveen Macherla

      Thanks for pointing the typo error.

      Can you post the stacktrace? If you don’t want to use MOXy for CDATA refer this for generating CDATA block with the default JAXB implementation in Java.

  • David


    I also tried this example, but book2.getDescription() returns a null. Am I missing anything? Thanks

    • http://blog.bdoughan.com Blaise Doughan

      Hi David,

      Below is a link to a Stack Overflow question that deals with unmarshalling the model from this post:
      - http://stackoverflow.com/a/15520878/383861

      I’m the MOXy lead and have have written the following post that may also help:
      - CDATA, CDATA Run, Run Data Run


      • http://theopentutorials.com Praveen Macherla

        Hi Blaise
        Welcome to theOpenTutorials :)

      • James Reader

        Do you have an example of how to add the @XMLCData annotation to a class that creates the text from a List content?

        I’m working with Onix for Books and there are several Text elements that store the data after unmarshalling in

        protected List content;

        It works fine unmarshalling, but it will not work with the CDATA annotation

    • http://theopentutorials.com Praveen Macherla

      David, follow the example as posted by Blaise in the stackoverflow link. It should work.

  • David


    I also followed the example, but book2.getDescription() returns a null. Am I missing anythign?

  • David

    It works. thanks

  • Jonathan

    Thanks for the help! I keep getting the following exception:

    javax.xml.bind.JAXBException: property “eclipselink-oxm-xml” is not supported

    My properties file is setup here:
    InputStream meta = Thread.currentThread().getContextClassLoader().getResourceAsStream(“oxm.xml”);
    Map metadata = new HashMap();
    metadata.put(“eclipselink-oxm-xml”, new StreamSource(meta));

    Any thoughts. I have oxm.xml file in the same directory as my source classes.