获取例外,而读元素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)

+0

工作对我来说,输出是: [email protected] 测试 – BretC

+0

感谢您的答复,其实这个问题是与loadFromXML()方法 – AMG

您正在尝试使用loadFromXML()方法从属性文件加载。我认为这是一个问题,因为stacktrace显示它没有得到所需的注释输入格式。

+0

感谢那些工作。 – AMG