获取例外,而读元素XML
我可以使用下面的代码成功加载基于XML的属性文件:获取例外,而读元素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:元素类型“属性”的内容必须匹配“(comment?,ent RY *)”。 (未知来源) at org.apache.xerces.util.ErrorHandlerWrapper.error ) 在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.sc anDocument(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 (Unknown Source) at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) XMLUtils.java:113) 在java.util.XMLUtils.load(XMLUtils.java:85)
工作对我来说,输出是: [email protected] 测试 – BretC
感谢您的答复,其实这个问题是与loadFromXML()方法 – AMG