Fatal Error :1:1: Content is not allowed in prolog

Viewed 126412

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

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.

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.

7 Answers

I wanted YAML for the log4j2 configuration file because it doffs XML's visual clutter, but had the same error as Guest96. I scoured the web for a solution to the above, investigating a Utf-8 BOM or other content in the YAML header area; no joy. Of course, the answer is usually simple.

Somewhere, I had fully missed it that using YAML with log4j2 required the jackson libraries, per https://www.sentinelone.com/blog/log4j2-configuration-detailed-guide/. Adding the jackson reference to my (Gradle) configuration fixed the problem:

// Gain support for log4j2.
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j
implementation 'org.apache.logging.log4j:log4j-api:2.14.1'
implementation 'org.apache.logging.log4j:log4j-core:2.14.1'

// Gain support for YAML with log4j2.
// https://www.sentinelone.com/blog/log4j2-configuration-detailed-guide/
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.10.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.0'

With that, the dreaded Fatal Error :1:1: Content is not allowed in prolog error went away.

The real solution that I found for this issue was by disabling any XML Format post processors. I have added a post processor called "jp@gc - XML Format Post Processor" and started noticing the error "Fatal Error :1:1: Content is not allowed in prolog"

By disabling the post processor had stopped throwing those errors.

It could be not supported file encoding. Change it to UTF-8 for example.

I've done this using Sublime

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

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

or

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