2017-10-11 6 views
1

私は古いcommons-configurationからcommons-configuration2に移行しようとしていますが、新しいConfigurationsビルダーを使用しているときにインデントでXML出力をフォーマットする際に問題があります。フォーマットXML出力/変更apacheのトランスフォーマcommons-configurations2

私がこれを好きになる前は、うまくいきました。

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

しかし、あなたはXMLConfigurationのサブクラスを作成する機能を削除しXMLConfigurationインスタンスを取得するためにConfigurationBuilderを使用コモンズ-configurations2に、このような例:

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

他の方法がありますXMLConfigurationのTransformerをカスタマイズするには?

ありがとうございます!

答えて

1

ここで私はそれをどのように解決したのですか。

XMLConfigurationを拡張する新しいクラスを作成します。

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

またはさらに簡単:

XMLConfiguration builder = new Configurations() 
    .fileBased(PrettyXMLConfiguration.class, new File("config.xml")); 
代わりに、このようなXMLConfigurationを作成

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

関連する問題