【问题标题】:Append CDATA using org.springframework.oxm jaxb2marshaller使用 org.springframework.oxm jaxb2marshaller 附加 CDATA
【发布时间】:2017-07-18 08:20:45
【问题描述】:

我在使用 jaxb2marshaller 使用 CDATA 将几个元素编组为 XML 时遇到了很大的麻烦。我已经通过了以下解决方案:

JAXB Marshalling Unmarshalling with CDATA

How to generate CDATA block using JAXB?

还有更多,但找不到合适的解决方案。 他们要么告诉切换到旧的 JAXB 实现,要么使用 MOXY。但是,这不是我的要求。我已经使用 OXM 库实现了以下两个类,并希望生成一个 XML,其中很少有元素需要附加 CDATA。

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

和

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

在主类中:

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

 }
}

还有一个简单的 POJO 类,其中包含用于生成 XML 的 setter 和 getter。

我可以在上面的任何地方进行一些更改以生成具有所需 CDATA 属性的 XML 吗?

注意:我已经尝试过 EclipseLink Moxy(@XmlData),但它没有与 OXM 集成。我希望在我的代码中不使用第三方 jar 来实现这一点。

【问题讨论】:

    标签: java spring spring-boot marshalling jaxb2-maven-plugin


    【解决方案1】:

    找到了 moxy 集成的解决方案(找不到任何其他方法),如果它可以帮助有需要的人,请在此处发布。

    导入 moxy 依赖并在创建 bean 的同一包中添加 jaxb.properties 文件,使用以下行:

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

    并将@XmlCDATA 注释放在必填字段上。 这会生成带有 CDATA 部分的 xml 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      相关资源
      最近更新 更多