2017-10-11 8 views
1

이전의 commons-configuration을 commons-configuration2로 마이그레이션하려고하는데 새로운 구성 빌더를 사용할 때 들여 쓰기로 XML 출력을 포맷하는 데 문제가 있습니다.형식 XML 출력/수정 아파치에서 공용 변환 -2 설정

전에 제대로 작동했는데.

XMLConfiguration configuration = new XMLConfiguration() 
{ 
    @Override 
    protected Transformer createTransformer() 
     throws ConfigurationException 
    { 
     Transformer transformer = super.createTransformer(); 
     transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
     transformer.setOutputProperty("http://xml.apache.org/xslt}indent-amount", "4"); 
     return transformer; 
    } 
}; 

그러나 평민 - configurations2에

당신은 다음과 같은 예를 들어, XMLConfiguration의 서브 클래스를 생성 할 수있는 기능을 제거하는 XMLConfiguration 인스턴스를 얻을 수있는 ConfigurationBuilder를 사용

XMLConfiguration configuration = configurations 
     .xmlBuilder(new File("config.xml")) 
     .getConfiguration(); 

다른 방법이 있나요 XMLConfiguration의 Transformer를 커스터마이즈하는 방법은?

감사합니다.

답변

1

어떻게 해결 했는가?

XMLConfiguration builder = new Configurations() 
     .fileBasedBuilder(PrettyXMLConfiguration.class, new File("config.xml")) 
     .getConfiguration(); 

또는 간단한 :

XMLConfiguration builder = new Configurations() 
    .fileBased(PrettyXMLConfiguration.class, new File("config.xml")); 

public class PrettyXMLConfiguration 
    extends XMLConfiguration 
{ 
    @Override 
    protected Transformer createTransformer() 
     throws ConfigurationException 
    { 
     Transformer transformer = super.createTransformer(); 
     transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
     transformer.setOutputProperty(
      "{http://xml.apache.org/xslt}indent-amount", "4"); 
     return transformer; 
    } 
} 

이 대신 같은 XMLConfiguration 만들기 :

는 XMLConfiguration이 확장하는 새 클래스를 만듭니다