【发布时间】:2016-01-11 07:47:39
【问题描述】:
我能够使用以下代码成功加载基于 XML 的属性文件:
Properties props = new Properties();
InputStream is = SampleConfig.class.getResourceAsStream("/test.properties");
System.out.println(is);
props.loadFromXML(is);
String appId = props.getProperty("favoriteSeason");
System.out.println(appId);
我的属性文件代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="favoriteSeason">test</entry>
<entry key="favoriteFruit">test1</entry>
<entry key="favoriteDay">test2</entry>
</properties>
但是,当我运行它时,我得到以下异常:
原因:org.xml.sax.SAXParseException:元素类型“properties”的内容必须匹配“(comment?,entry*)”。 在 org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(未知来源) 在 org.apache.xerces.util.ErrorHandlerWrapper.error(未知来源) 在 org.apache.xerces.impl.XMLErrorReporter.reportError(未知来源) 在 org.apache.xerces.impl.XMLErrorReporter.reportError(未知来源) 在 org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(未知来源) 在 org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(未知来源) 在 org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(未知来源) 在 org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(未知来源) 在 org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(未知来源) 在 org.apache.xerces.parsers.XML11Configuration.parse(未知来源) 在 org.apache.xerces.parsers.XML11Configuration.parse(未知来源) 在 org.apache.xerces.parsers.XMLParser.parse(未知来源) 在 org.apache.xerces.parsers.DOMParser.parse(未知来源) 在 org.apache.xerces.jaxp.DocumentBuilderImpl.parse(未知来源) 在 java.util.XMLUtils.getLoadingDoc(XMLUtils.java:113) 在 java.util.XMLUtils.load(XMLUtils.java:85)
【问题讨论】:
-
为我工作,输出为:java.io.BufferedInputStream@3a09a47d test
-
感谢回复,其实问题出在 loadFromXML() 方法上