java.util.InvalidPropertiesFormatException:SAXParseException

时间:2012-12-11 17:34:29

标签: java xml qpid

我正在尝试将Apache Qpid实现到我们的架构中,但我在阅读属性文件时遇到了问题。

  

属性props = new Properties();             props.loadFromXML(this.getClass()的getResourceAsStream( “hello.properties”));

loadfromxml给我一个错误,这是堆栈跟踪:

> java.util.InvalidPropertiesFormatException:
> org.xml.sax.SAXParseException: Content is not allowed in prolog.  at
> java.util.XMLUtils.load(XMLUtils.java:56)     at
> java.util.Properties.loadFromXML(Properties.java:852)     at
> com.irad.message.system.HelloTest.runTest(HelloTest.java:29)  at
> com.irad.message.system.HelloTest.main(HelloTest.java:23) Caused by:
> org.xml.sax.SAXParseException: Content is not allowed in prolog.  at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
>   at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
>   at
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
>   at
> com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1427)
>   at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1036)
>   at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
>   at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
>   at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
>   at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
>   at
> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
>   at
> com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232)
>   at
> com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
>   at java.util.XMLUtils.getLoadingDoc(XMLUtils.java:82)   at
> java.util.XMLUtils.load(XMLUtils.java:54)     ... 3 more

这是hello.properties文件:

java.naming.factory.initial=org.apache.qpid.jndi.PropertiesFileInitialContextFactory
connectionfactory.qpidConnectionfactory= amqp://guest:guest@clientid/test?brokerlist='tcp://localhost:5672'
destination.topicExchange = amq.topic

我做错了什么?我从Qpid的文档页面复制了代码片段。

1 个答案:

答案 0 :(得分:1)

您正在使用Properties.loadFromXML将TEXT文件作为XML加载,它告诉您您提供的XML文件格式错误。

下次当您收到此类错误时,请搜索“java loadfromxml”。它将带你到javadocs手册上如何使用它,你会看到你喂它的参数不正确:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Properties.html#loadFromXML%28java.io.InputStream%29

以上链接说:

XML文档必须具有以下DOCTYPE声明:

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

将您的属性文件更改为正确格式,然后您就可以找出下一个错误了。