如何按需创建 XMLConfiguration

How to create an XMLConfiguration on demand

我正在使用 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 文件副本)

我在应该使用 portal(0).name 的地方使用了 portal.0.name,这是通过...解决的...阅读 the doc