Java 读取XML元素时获取异常
我能够使用以下代码成功加载基于XML的属性文件:Java 读取XML元素时获取异常,java,xml,saxparser,Java,Xml,Saxparser,我能够使用以下代码成功加载基于XML的属性文件: Properties props = new Properties(); InputStream is = SampleConfig.class.getResourceAsStream("/test.properties"); System.out.println(is); props.loadFromXML(is); String appId = props.getProperty("favoriteSe
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:元素类型属性的内容必须与comment?中的条目*匹配。
位于org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseExceptionUnknown源
位于org.apache.xerces.util.ErrorHandlerWrapper.errorUnknown Source
位于org.apache.xerces.impl.XMLErrorReporter.reportErrorUnknown Source
位于org.apache.xerces.impl.XMLErrorReporter.reportErrorUnknown Source
位于org.apache.xerces.impl.dtd.XMLDTDValidator.handleendelement未知源
位于org.apache.xerces.impl.dtd.XMLDTDValidator.endElementUnknown Source
位于org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.ScannedlementUnknown Source
位于org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.Dispatch未知源
位于org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocumentUnknown Source
位于org.apache.xerces.parsers.XML11Configuration.parseUnknown Source
位于org.apache.xerces.parsers.XML11Configuration.parseUnknown Source
位于org.apache.xerces.parsers.XMLParser.parseUnknown Source
位于org.apache.xerces.parsers.DOMParser.parseUnknown Source
位于org.apache.xerces.jaxp.DocumentBuilderImpl.parseUnknown Source
位于java.util.XMLUtils.getLoadingDocXMLUtils.java:113
位于java.util.XMLUtils.loadXMLUtils.java:85
您正在尝试使用loadFromXML方法从.properties文件加载。我认为这就是问题所在,因为stacktrace显示它没有获得所需格式的注释条目类型。对我有效,输出为:java.io。BufferedInputStream@3a09a47dtest谢谢你的回复,实际上问题出在loadFromXML方法上