Append CDATA using org.springframework.oxm jaxb2marshaller
Asked Answered
I

1

9

I am having big trouble while marshaling few elements to XML with CDATA using jaxb2marshaller. I have gone through the solutions like:

JAXB Marshalling Unmarshalling with CDATA

How to generate CDATA block using JAXB?

and much more, but could not find a proper solution. They either tell to switch to old JAXB implementation or use MOXY. But, this is not my requirement. I have implemented below two classes using OXM library and want to generate an XML where few elements need to have CDATA appended.

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

and

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

In Main class:

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

 }
}

and a simple POJO class with setters and getters to produce the XML.

Can I do some changes anywhere above to produce the XML with required CDATA attribute?

NOTE: I already tried EclipseLink Moxy(@XmlData) and it does not integrate with OXM. I am looking to implement this without using a third party jar in my code.

Ib answered 27/2, 2017 at 16:13 Comment(0)
I
4

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.

Ib answered 3/3, 2017 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.