如何按需创建XMLConfiguration

时间:2015-02-06 10:55:21

标签: java apache-commons-config

我正在使用commons-configuration 1.10,并希望仅在需要时创建我的配置文件。

现在,我有

        XMLConfiguration config= new XMLConfiguration(file);
        config.setReloadingStrategy(new FileChangedReloadingStrategy());
        config.setAutoSave(true);

但是,当我试着打电话时

        config.setProperty("portal.0.name", portal.getName());

我得到了

Caused by: org.apache.commons.configuration.ConfigurationException: org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. 
    at org.apache.commons.configuration.XMLConfiguration.createDocument(XMLConfiguration.java:914)
    at org.apache.commons.configuration.XMLConfiguration.save(XMLConfiguration.java:1034)
    at org.apache.commons.configuration.AbstractHierarchicalFileConfiguration$FileConfigurationDelegate.save(AbstractHierarchicalFileConfiguration.java:570)
    at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:557)
    at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:524)
    at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:474)
    at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:441)
    at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:418)
    at org.apache.commons.configuration.AbstractFileConfiguration.possiblySave(AbstractFileConfiguration.java:749)
    ... 29 more
Caused by: org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. 
    at org.apache.xerces.dom.CoreDocumentImpl.createElement(Unknown Source)
    at org.apache.commons.configuration.XMLConfiguration$XMLBuilderVisitor.insert(XMLConfiguration.java:1529)
    at org.apache.commons.configuration.HierarchicalConfiguration$BuilderVisitor.visitBeforeChildren(HierarchicalConfiguration.java:1734)
    at org.apache.commons.configuration.HierarchicalConfiguration$Node.visit(HierarchicalConfiguration.java:1401)
    at org.apache.commons.configuration.HierarchicalConfiguration$Node.visit(HierarchicalConfiguration.java:1407)
    at org.apache.commons.configuration.XMLConfiguration$XMLBuilderVisitor.processDocument(XMLConfiguration.java:1504)
    at org.apache.commons.configuration.XMLConfiguration.createDocument(XMLConfiguration.java:908)
    ... 37 more

这似乎表明文件无法通过自动保存机制保存。

那么,我有什么不好的事吗?

如何在仅在需要时创建文件时创建配置(否则将需要一些奇怪的XML文件副本)

1 个答案:

答案 0 :(得分:0)

我使用了portal.0.name,我应该使用portal(0).name,这已经通过......嗯...阅读the doc来解决。