我有一个基于Java的Web服务客户端连接到Java Web服务(在Axis1框架上实现).
我在日志文件中遇到以下异常:
Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(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.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.ws.axis.security.WSDoAllReceiver.invoke(WSDoAllReceiver.java:114)
at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:198)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
Run Code Online (Sandbox Code Playgroud)
Mik*_*lov 225
这通常是由XML声明之前的空格引起的,但它可以是任何文本,如破折号或任何字符.我说经常是由白色空间引起的,因为人们认为白色空间总是可以忽略不计,但事实并非如此.
经常发生的另一件事是UTF-8 BOM(字节顺序标记),如果将文档作为字符流传递给XML解析器而不是作为字节流,则可以将XML声明视为空格之前允许.
如果使用模式文件(.xsd)来验证xml文件,并且其中一个模式文件具有UTF-8 BOM,则会发生同样的情况.
小智 29
实际上除了Yuriy Zubarev的帖子
将不存在的xml文件传递给解析器时.例如,你通过
new File("C:/temp/abc")
Run Code Online (Sandbox Code Playgroud)
当您的文件系统上只存在C:/temp/abc.xml文件时
在任一情况下
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = builder.parse(new File("C:/temp/abc"));
Run Code Online (Sandbox Code Playgroud)
要么
DOMParser parser = new DOMParser();
parser.parse("file:C:/temp/abc");
Run Code Online (Sandbox Code Playgroud)
所有都给出相同的错误消息.
非常令人失望的bug,因为下面的痕迹
javax.servlet.ServletException
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
...
Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
... 40 more
Run Code Online (Sandbox Code Playgroud)
没有说'文件名不正确'或'这样的文件不存在'的事实.在我的情况下,我有绝对正确的xml文件,不得不花了2天来确定真正的问题.
har*_*ath 26
尝试encoding="UTF-8"在prolog中的字符串和终止之间添加一个空格?>.在XML中,prolog在文档的开头指定这个括号 - 问号分隔元素(而stackoverflow中的标记prolog指的是编程语言).
补充:这个破折号是在文档的prolog部分前面吗?那就是那里的错误,在prolog面前有数据,-<?xml version="1.0" encoding="UTF-8"?>.
小智 11
尝试使用freemarker解析XML文档时,我遇到了同样的问题(并解决了它).
我在XML文件的标题之前没有空格.
当且仅当文件编码和XML编码属性不同时才会出现此问题.(例如:头文件中带有UTF-16属性的UTF-8文件).
所以我有两种解决问题的方法:
花了4个小时跟踪WSDL中的类似问题.结果是WSDL使用了一个导入另一个命名空间XSD的XSD.此导入的XSD包含以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.xyz.com/Services/CommonTypes" elementFormDefault="qualified"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:CommonTypes="http://www.xyz.com/Services/CommonTypes">
<include schemaLocation=""></include>
<complexType name="RequestType">
<....
Run Code Online (Sandbox Code Playgroud)
注意空include元素!这是我的困境的根源.我猜这是上面没有找到Egor文件的变种.
+1令人失望的错误报告.
我的回答可能对你没有帮助,但它通常有助于解决这个问题。
当你看到这种异常时,你应该尝试在任何十六进制编辑器中打开你的 xml 文件,有时你可以在文件的开头看到文本编辑器没有显示的额外字节。
删除它们,您的 xml 将被解析。
| 归档时间: |
|
| 查看次数: |
516439 次 |
| 最近记录: |