致命错误:1:1:prolog中不允许内容

时间:2023-01-15 07:25:17

I'm using Java and i'm trying to get XML document from some http link. Code I'm using is:

我正在使用Java,我正试图从一些http链接获取XML文档。我正在使用的代码是:

URL url = new URL(link);

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
Document doc = null;

CountInputStream in = new CountInputStream(url.openStream());
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);

Don't pay attention at CountInputStream, it's some special class acting like regular input stream.

不要注意CountInputStream,它是一些像常规输入流一样的特殊类。

Using the code above, I sometimes got error Fatal Error :1:1: Content is not allowed in prolog. I assume that is has something to do with bad format of xml, but I have no idea how to fix it.

使用上面的代码,我有时会收到错误致命错误:1:1:prolog中不允许使用内容。我认为这与xml格式不好有关,但我不知道如何修复它。

4 个解决方案

#1


18  

I'm turning my comment to an answer, so it can be accepted and this question no longer remains unanswered.

我正在将我的评论转为答案,因此可以接受,这个问题不再仍然没有答案。

The most likely cause of this is a malformed response, which includes characters before the initial <?xml …>. So please have a look at the document as transferred over HTTP, and fix this on the server side.

最可能的原因是格式错误的响应,其中包括初始<?xml ...>之前的字符。因此,请查看通过HTTP传输的文档,并在服务器端修复此问题。

#2


6  

There are certainly some weird characters (e.g. BOM) or some whitespace before the XML preamble (<?xml ...?>)?

在XML前导码(<?xml ...?>)之前肯定有一些奇怪的字符(例如BOM)或一些空格?

#3


0  

Someone should mark Johannes Weiß's comment as the answer to this question. That is exactly why xml documents can't just be loaded in a DOM Document class.

有人应该将JohannesWeiß的评论标记为这个问题的答案。这正是为什么xml文档不能只在DOM Document类中加载的原因。

http://en.wikipedia.org/wiki/Byte_order_mark

http://en.wikipedia.org/wiki/Byte_order_mark

#4


0  

Looks like you forgot adding correct headers to your get request (ask the REST API developer or you specific API description):

看起来您忘记在get请求中添加正确的标头(请询问REST API开发人员或您的具体API说明):

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.header("Accept", "application/xml")
connection.setRequestMethod("GET");
connection.connect();

or

要么

connection.header("Accept", "application/xml;version=1")

#1


18  

I'm turning my comment to an answer, so it can be accepted and this question no longer remains unanswered.

我正在将我的评论转为答案,因此可以接受,这个问题不再仍然没有答案。

The most likely cause of this is a malformed response, which includes characters before the initial <?xml …>. So please have a look at the document as transferred over HTTP, and fix this on the server side.

最可能的原因是格式错误的响应,其中包括初始<?xml ...>之前的字符。因此,请查看通过HTTP传输的文档,并在服务器端修复此问题。

#2


6  

There are certainly some weird characters (e.g. BOM) or some whitespace before the XML preamble (<?xml ...?>)?

在XML前导码(<?xml ...?>)之前肯定有一些奇怪的字符(例如BOM)或一些空格?

#3


0  

Someone should mark Johannes Weiß's comment as the answer to this question. That is exactly why xml documents can't just be loaded in a DOM Document class.

有人应该将JohannesWeiß的评论标记为这个问题的答案。这正是为什么xml文档不能只在DOM Document类中加载的原因。

http://en.wikipedia.org/wiki/Byte_order_mark

http://en.wikipedia.org/wiki/Byte_order_mark

#4


0  

Looks like you forgot adding correct headers to your get request (ask the REST API developer or you specific API description):

看起来您忘记在get请求中添加正确的标头(请询问REST API开发人员或您的具体API说明):

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.header("Accept", "application/xml")
connection.setRequestMethod("GET");
connection.connect();

or

要么

connection.header("Accept", "application/xml;version=1")