首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用org.springframework.oxm jaxb2marshaller追加CDATA

使用org.springframework.oxm jaxb2marshaller追加CDATA
EN

Stack Overflow用户
提问于 2017-02-27 16:13:54
回答 1查看 2.5K关注 0票数 9

在使用jaxb2marshaller使用CDATA将少数元素编组到XML时,我遇到了很大的困难。我经历过这样的解决方案:

JAXB Marshalling Unmarshalling with CDATA

How to generate CDATA block using JAXB?

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

代码语言:javascript
复制
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;
    }
} 

代码语言:javascript
复制
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();
        }
    }
} 

主修班:

代码语言:javascript
复制
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);

 }
}

和一个带有setters和getter的简单POJO类来生成XML。

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-03 11:50:20

找到了用moxy集成的解决方案(找不到其他方法),如果它帮助了需要帮助的人,就在这里发布。

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

代码语言:javascript
复制
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

并将@XmlCDATA注释放在所需字段上。这将生成带有CDATA节的xml文件。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42490517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档