2017-02-27 1 views
8

jaxb2marshaller를 사용하여 CDATA로 XML에 몇 가지 요소를 정렬하는 데 큰 어려움을 겪고 있습니다. 나는 같은 솔루션을 통해 갈 :org.springframework.oxm을 사용하여 CDATA를 추가하십시오. jaxb2marshaller

JAXB Marshalling Unmarshalling with CDATA

How to generate CDATA block using JAXB?

그리고 더 많은,하지만 적절한 해결책을 찾을 수 없습니다. 이전 JAXB 구현으로 전환하거나 MOXY를 사용합니다. 그러나 이것은 나의 요구 사항이 아닙니다. 필자는 OXM 라이브러리를 사용하여 두 개의 클래스를 구현했으며, CDATA를 추가 할 필요가없는 XML을 생성하려고합니다.

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

} 
} 

과 XML을 생성하는 세터와 게터 간단한 POJO 클래스입니다.

필수 CDATA 속성이있는 XML을 생성하기 위해 위의 몇 가지 변경 사항을 수행 할 수 있습니까?

참고 : 이미 EclipseLink Moxy (@XmlData)를 시도했지만 OXM과 통합되지 않았습니다. 내 코드에서 제 3 자 jar를 사용하지 않고 이것을 구현하려고합니다.

답변

4

Moxy 통합 솔루션을 찾았습니다. (다른 방법으로는 찾을 수 없었습니다.) 도움이 필요하면 여기에 게시하십시오.

수입 MOXY 의존성과 콩이 다음 행으로 작성됩니다 동일한 패키지에 추가 jaxb.properties 파일 :

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

및 필수 필드 @XmlCDATA 주석을 넣어. 이것은 CDATA 섹션이있는 xml 파일을 생성했습니다.

관련 문제