java org.xml.sax.SAXParseException:序言中不允许内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7569089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.xml.sax.SAXParseException: Content is not allowed in prolog
提问by a CVn
Yes, I know that general forms of this question have been asked time and time again. However, I couldn't find anything that helped me solve my problem, so am posting this question which is specifically about my problem.
是的,我知道这个问题的一般形式已经一次又一次地被问到。但是,我找不到任何可以帮助我解决问题的内容,因此我发布了这个专门针对我的问题的问题。
I am trying to figure out why I am getting a SAXParseException
(Content is not allowed in prolog.
) as the OpenSAML library is trying to parse some XML. The most useful hints I found pointed toward an errant BOM at the beginning of the file, but there's nothing like that. I also wrote a quick-and-dirty C#.NET routine to read the whole file as an array of bytes, iterate over it and tell me if any of them were >=0x80 (it found none). The XML is marked as utf-8. I am hoping that someone can provide me with a bit of insight as to what might be going wrong.
我试图弄清楚为什么我得到SAXParseException
( Content is not allowed in prolog.
) 因为 OpenSAML 库试图解析一些 XML。我发现的最有用的提示指向文件开头的错误 BOM,但没有类似的东西。我还编写了一个快速而肮脏的 C#.NET 例程,将整个文件作为一个字节数组读取,迭代它并告诉我它们中是否有 >=0x80(它没有找到)。XML 标记为 utf-8。我希望有人可以让我对可能出现的问题有所了解。
The initial portion of the XML file, as a hex dump, is (note the use of 0A
as a newline; removing the line feed character entirely has no apparent effect):
作为十六进制转储的 XML 文件的初始部分是(注意0A
用作换行符;完全删除换行符没有明显效果):
000000000 3C 3F 78 6D 6C 20 76 65-72 73 69 6F 6E 3D 22 31 |<?xml version="1|
000000010 2E 30 22 20 65 6E 63 6F-64 69 6E 67 3D 22 55 54 |.0" encoding="UT|
000000020 46 2D 38 22 3F 3E 0A 3C-6D 64 3A 45 6E 74 69 74 |F-8"?>.<md:Entit|
000000030 79 44 65 73 63 72 69 70-74 6F 72 20 78 6D 6C 6E |yDescriptor xmln|
000000040 73 3A 6D 64 3D 22 75 72-6E 3A 6F 61 73 69 73 3A |s:md="urn:oasis:|
000000050 6E 61 6D 65 73 3A 74 63-3A 53 41 4D 4C 3A 32 2E |names:tc:SAML:2.|
000000060 30 3A 6D 65 74 61 64 61-74 61 22 20 |0:metadata" |
The stack trace for the root cause exception is:
根本原因异常的堆栈跟踪是:
org.xml.sax.SAXParseException: Content is not allowed in prolog.
org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
org.opensaml.xml.parse.BasicParserPool$DocumentBuilderProxy.parse(BasicParserPool.java:665)
my.Unmarshaller.unmarshall(Unmarshaller.java:39)
... internal calls omitted for brevity ...
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
The code that tries to do the unmarshalling is (type names fully qualified here; hopefully I am not leaving out something important):
试图进行解组的代码是(类型名称在这里完全限定;希望我没有遗漏一些重要的东西):
package my;
public class Unmarshaller {
protected static org.opensaml.xml.parse.ParserPool parserPool;
static {
org.opensaml.xml.parse.BasicParserPool _parserPool;
_parserPool = new org.opensaml.xml.parse.BasicParserPool();
_parserPool.setNamespaceAware(true);
Unmarshaller.parserPool = _parserPool;
}
public Unmarshaller() {
try {
org.opensaml.DefaultBootstrap.bootstrap();
} catch (org.opensaml.xml.ConfigurationException e) {
throw new java.lang.RuntimeException (e);
}
}
public Object unmarshall(String xml)
throws org.opensaml.xml.io.UnmarshallingException {
assert xml != null;
assert !xml.isEmpty();
assert Unmarshaller.parserPool != null;
org.w3c.dom.Document doc;
try {
doc =
(parserPool.getBuilder())
.parse( // <<<====== line 39 in original source code is here
new org.xml.sax.InputSource(
new java.io.StringReader(xml)
)
);
} catch (org.xml.sax.SAXException e) {
throw new org.opensaml.xml.io.UnmarshallingException(e);
} catch (java.io.IOException e) {
throw new org.opensaml.xml.io.UnmarshallingException(e);
} catch (org.opensaml.xml.parse.XMLParserException e) {
throw new org.opensaml.xml.io.UnmarshallingException(e);
}
// ... remainder of function omitted for brevity ...
}
}
回答by Stephen C
I can't see anything wrong with the XML fragment in the file dump. And I believe you when you say that the XML file validates.
我看不出文件转储中的 XML 片段有什么问题。当您说 XML 文件有效时,我相信您。
However, you have not presented water-tight evidence that the XML that the parser seesis valid. For instance:
但是,您没有提供绝对可靠的证据来证明解析器看到的 XML是有效的。例如:
You might be trying to parse a different file to the one that you have dumped. (These things have been known to happen ...).
Alternatively, there might be something wrong with the way that you are getting the XML into that String that you then parse.
您可能正在尝试将不同的文件解析为已转储的文件。(这些事情已经知道会发生......)。
或者,将 XML 放入随后解析的字符串的方式可能有问题。
Try dumping the first few lines of the String that provides the parser source stream.
尝试转储提供解析器源流的字符串的前几行。
回答by cbheema
There are cases where the SAMLReponses are Base64 encoded, the consumers (Controller or Servlets) need to decode in order to resolve the issue of "Content is not allowed in prolog"
在某些情况下,SAMLReponses 是 Base64 编码的,消费者(控制器或 Servlets)需要解码以解决“序言中不允许内容”的问题
Below is the code snippet:
下面是代码片段:
// code snippet
String responseMessage = httpServletRequest.getParameter("SAMLResponse");
byte[] decoded = Base64.decode(responseMessage);
ByteArrayInputStream is = new ByteArrayInputStream(decoded);
// write your xml parsing logic here.