如何使用java从配置文件中检索键值

时间:2017-04-21 07:17:36

标签: java

我试图在java.Im中读取webconfig文件,但收到以下错误。

java.util.InvalidPropertiesFormatException: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 16; Document root element "configuration", must match DOCTYPE root "null".
    at sun.util.xml.PlatformXmlPropertiesProvider.load(PlatformXmlPropertiesProvider.java:80)
    at java.util.Properties$XmlSupport.load(Properties.java:1201)
    at java.util.Properties.loadFromXML(Properties.java:881)
    at Extensions.Utilities.ReadValueFromXmlPropertyFile(Utilities.java:46)
    at Extensions.Utilities.main(Utilities.java:30)
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 16; Document root element "configuration", must match DOCTYPE root "null".
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.rootElementSpecified(Unknown Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentScannerImpl$ContentDispatcher.scanRootElementHook(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at sun.util.xml.PlatformXmlPropertiesProvider.getLoadingDoc(PlatformXmlPropertiesProvider.java:106)
    at sun.util.xml.PlatformXmlPropertiesProvider.load(PlatformXmlPropertiesProvider.java:78)
    ... 4 more

这是我的代码:

 public static String ReadValueFromXmlPropertyFile(String Key,
                    String Filename) {
                String KeyValue = null;
                try {
                //  File file = new File(userDir+"/"+ Filename);
                    File file = new File(Filename);
                    FileInputStream fileInput = new FileInputStream(file);

                    Properties prop = new Properties();
                    prop.loadFromXML(fileInput);

                    KeyValue = prop.getProperty(Key);

                    System.out.println(Key+"-->"+KeyValue);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return KeyValue;
            }

,这是Web配置文件内容:

<add key="DateOfBirthPopUp" value="False"/>
<add key="DOBMaxAge" value="19"/>
<add key="DOBMinAge" value="12"/>

并且网络配置文件没有

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

1 个答案:

答案 0 :(得分:0)

将webconfig.xml更改为以下并尝试

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Here are some favorites</comment>
<entry key="DateOfBirthPopUp">False</entry>
<entry key="DOBMaxAge">19</entry>
<entry key="DOBMinAge">12</entry>
</properties>

您可能想阅读this