简体   繁体   English

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

[英]How to retrieve the key value from configuration file using java

I tried to read webconfig file in java.Im getting the below error. 我试图在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

This is my code: 这是我的代码:

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

and This is web config file contents: 这是Web配置文件的内容:

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

and web config file does'nt have 和Web配置文件没有

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

Change the webconfig.xml to below and try, 将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>

May be you want to read this . 可能是您想阅读这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM