J'ai de gros problèmes en marshalant quelques éléments en XML avec CDATA en utilisant jaxb2marshaller. Je suis passé par les solutions comme:Ajout de CDATA en utilisant org.springframework.oxm jaxb2marshaller
JAXB Marshalling Unmarshalling with CDATA
How to generate CDATA block using JAXB?
et beaucoup plus, mais ne pouvait pas trouver une solution appropriée. Ils disent soit de passer à l'ancienne implémentation JAXB, soit d'utiliser MOXY. Mais, ce n'est pas mon exigence. J'ai implémenté ci-dessous deux classes en utilisant la bibliothèque OXM et je veux générer un XML où peu d'éléments doivent avoir CDATA ajouté.
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
public class AppConfig {
@Bean
public Processor getHandler(){
Processor handler= new Processor();
handler.setMarshaller(getCastorMarshaller());
handler.setUnmarshaller(getCastorMarshaller());
return handler;
}
@Bean
public Jaxb2Marshaller getCastorMarshaller() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setPackagesToScan("com.pom.dom.whatever.model");
Map<String,Object> map = new HashMap<String,Object>();
map.put("jaxb.formatted.output", true);
jaxb2Marshaller.setMarshallerProperties(map);
return jaxb2Marshaller;
}
}
et
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
public class Processor {
private Marshaller marshaller;
private Unmarshaller unmarshalling;
public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}
public void setUnmarshaller(Unmarshaller unmarshalling) {
this.unmarshaller = unmarshaller;
}
//Converts Object to XML file
public void objectToXML(String fileName, Object graph) throws IOException {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fileName);
marshaller.marshal(graph, new StreamResult(fos));
} finally {
fos.close();
}
}
//Converts XML to Java Object
public Object xmlToObject(String fileName) throws IOException {
FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);
return unmarshaller.unmarshal(new StreamSource(fis));
} finally {
fis.close();
}
}
}
En classe principale:
generateXML(){
public void generateCheckXML(ReportDTO repDTO, String fileName){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
Processor processor = ctx.getBean(Processor.class);
ObjectFactory objectFactory = new ObjectFactory();
TRIMSInterface trimsInterface = objectFactory.createTRIMSInterface();
// setters
processor.objectToXML(fileName,trimsInterface);
}
}
et une simple classe POJO avec setters et getters pour produire le XML. Est-ce que je peux faire des changements n'importe où ci-dessus pour produire le XML avec l'attribut CDATA requis?
NOTE: J'ai déjà essayé EclipseLink Moxy (@XmlData) et il ne s'intègre pas avec OXM. Je cherche à implémenter ceci sans employer un pot de tiers dans mon code.