如何使用Apache Commons Configuration2从InputStream中读取数据?
FileBasedConfigurationBuilder<XMLConfiguration> builder =
new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class)
.configure(
new Parameters()
.xml()
.setFileName("")
.setExpressionEngine(new XPathExpressionEngine())
);
XMLConfiguration config = builder.getConfiguration();
config.read(sourceJarFile.getInputStream(sourcePropertiesEntry))
给出上面的代码,如果setFileName被赋予空字符串,我将得到以下异常。
org.apache.commons.configuration2.ex.ConfigurationException: Could not locate: org.apache.commons.configuration2.io.FileLocator@61dc03ce[fileName=tmp.xml,basePath=<null>,sourceURL=,encoding=<null>,fileSystem=<null>,locationStrategy=<null>]
at org.apache.commons.configuration2.io.FileLocatorUtils.locateOrThrow(FileLocatorUtils.java:346)
at org.apache.commons.configuration2.io.FileHandler.load(FileHandler.java:972)
at org.apache.commons.configuration2.io.FileHandler.load(FileHandler.java:702)
at org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder.initFileHandler(FileBasedConfigurationBuilder.java:312)
at org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder.initResultInstance(FileBasedConfigurationBuilder.java:291)
at org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder.initResultInstance(FileBasedConfigurationBuilder.java:60)
at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.createResult(BasicConfigurationBuilder.java:421)
at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.getConfiguration(BasicConfigurationBuilder.java:285)
at com.test.installer.App.getXMLConfigurationProperties(App.java:185)
如果我给null或者只是不调用setFileName();我将无法在read()行加载配置异常。
org.apache.commons.configuration2.ex.ConfigurationException: Unable to load the configuration
at org.apache.commons.configuration2.XMLConfiguration.load(XMLConfiguration.java:986)
at org.apache.commons.configuration2.XMLConfiguration.read(XMLConfiguration.java:954)
at com.test.installer.App.updateExistedProperties(App.java:84)
答案 0 :(得分:2)
来自example in the API documentation:
设置文件参数(编码等):
FileBasedBuilderParameters fileparams = ...
FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class).configure(fileparams);
然后:
FileBasedConfiguration config = builder.getConfiguration();
FileHandler fileHandler = new FileHandler(config);
Inputstream istream = ...
fileHandler.load(istream);
请注意,您无法使用自动保存。为了节省您的费用,可能需要提供OutputStream
。类似的东西:
fh.save(ostream)
答案 1 :(得分:1)
从输入流(在commons-collections 2.x中)加载XML配置数据的正确方法如下:
XMLConfiguration cfg = new BasicConfigurationBuilder<>(XMLConfiguration.class).configure(new Parameters().xml()).getConfiguration();
FileHandler fh = new FileHandler(cfg);
fh.load(inputStream);
调用load()
cfg
后将包含已加载的配置。
另请注意,不应使用XMLConfiguration.read()方法,因为此方法是为内部使用而设计的,将来可能会重命名为_read()(请参阅:https://issues.apache.org/jira/browse/CONFIGURATION-641)。
答案 2 :(得分:0)
您可以使用XMLConfiguration.read(InputStream in)
,但据我所知,您需要在某处拥有XML文件。原因是当您从构建器获取配置或调用上面的read
方法时,私有load
方法中有一些检查(XMLConfiguration.java in the source files中的第963行)。
Parameters params = new Parameters();
FileBasedConfigurationBuilder<XMLConfiguration> fileBuilder =
new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
.configure(params.fileBased().setFileName("/tmp/dummy.xml"));`
XMLConfiguration xmlConfiguration = fileBuilder.getConfiguration();
xmlConfiguration.read(inputStream);
虚拟文件可以是任何东西,只要它的格式良好,它就不需要有效。就我而言,/tmp/dummy.xml
只包含<_/>
。