How to Generate Cdata Block Using Jaxb

JAXB Marshalling Unmarshalling with CDATA

You could do the following:

AdapterCDATA

package forum14193944;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class AdapterCDATA extends XmlAdapter<String, String> {

@Override
public String marshal(String arg0) throws Exception {
return "<![CDATA[" + arg0 + "]]>";
}
@Override
public String unmarshal(String arg0) throws Exception {
return arg0;
}

}

Root

The @XmlJavaTypeAdapter annotation is used to specify that the XmlAdapter should be used.

package forum14193944;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

@XmlJavaTypeAdapter(AdapterCDATA.class)
private String name;

@XmlJavaTypeAdapter(AdapterCDATA.class)
private String surname;

@XmlJavaTypeAdapter(AdapterCDATA.class)
private String id;

}

Demo

I had to wrap System.out in an OutputStreamWriter to get the desired effect. Also note that setting a CharacterEscapeHandler means that it is responsible for all escape handling for that Marshaller.

package forum14193944;

import java.io.*;
import javax.xml.bind.*;
import com.sun.xml.bind.marshaller.*;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum14193944/input.xml");
Root root = (Root) unmarshaller.unmarshal(xml);

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(CharacterEscapeHandler.class.getName(),
new CharacterEscapeHandler() {
@Override
public void escape(char[] ac, int i, int j, boolean flag,
Writer writer) throws IOException {
writer.write(ac, i, j);
}
});
marshaller.marshal(root, new OutputStreamWriter(System.out));
}

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<name><![CDATA[<h1>kshitij</h1>]]></name>
<surname><![CDATA[<h1>solanki</h1>]]></surname>
<id><![CDATA[0]]></id>
</root>

Append CDATA using org.springframework.oxm jaxb2marshaller

Found the solution with moxy integration (could not find any other way around), posting here if it helps someone in need.

imported moxy dependency and added jaxb.properties file in the same package where bean is created with the following line:

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

and put @XmlCDATA annotation on the required field.
This generated the xml file with CDATA sections.

JAXB How to append and XML String into a XML element?

Elaborating on my comment - JAXB by default escapes any text that you set on element.

You can disable escaping using one of solutions described in How to prevent JAXB escaping a string

However, I see that what you really need is just putting text in CDATA section. This can be achieved using one of solutions described here: How to generate CDATA block using JAXB?



Related Topics



Leave a reply



Submit