I’ve been beating my head against this absolutely infuriating bug for the last 48 hours, so I thought I’d finally throw in the towel and try asking here before I throw my laptop out the window.
I’m trying to parse the response XML from a call I made to AWS SimpleDB. The response is coming back on the wire just fine; for example, it may look like:
<?xml version="1.0" encoding="utf-8"?>
<ListDomainsResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/">
<ListDomainsResult>
<DomainName>Audio</DomainName>
<DomainName>Course</DomainName>
<DomainName>DocumentContents</DomainName>
<DomainName>LectureSet</DomainName>
<DomainName>MetaData</DomainName>
<DomainName>Professors</DomainName>
<DomainName>Tag</DomainName>
</ListDomainsResult>
<ResponseMetadata>
<RequestId>42330b4a-e134-6aec-e62a-5869ac2b4575</RequestId>
<BoxUsage>0.0000071759</BoxUsage>
</ResponseMetadata>
</ListDomainsResponse>
I pass in this XML to a parser with
XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(response.getContent());
and call eventReader.nextEvent(); a bunch of times to get the data I want.
Here’s the bizarre part — it works great inside the local server. The response comes in, I parse it, everyone’s happy. The problem is that when I deploy the code to Google App Engine, the outgoing request still works, and the response XML seems 100% identical and correct to me, but the response fails to parse with the following exception:
com.amazonaws.http.HttpClient handleResponse: Unable to unmarshall response (ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.): <?xml version="1.0" encoding="utf-8"?>
<ListDomainsResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/"><ListDomainsResult><DomainName>Audio</DomainName><DomainName>Course</DomainName><DomainName>DocumentContents</DomainName><DomainName>LectureSet</DomainName><DomainName>MetaData</DomainName><DomainName>Professors</DomainName><DomainName>Tag</DomainName></ListDomainsResult><ResponseMetadata><RequestId>42330b4a-e134-6aec-e62a-5869ac2b4575</RequestId><BoxUsage>0.0000071759</BoxUsage></ResponseMetadata></ListDomainsResponse>
javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(Unknown Source)
at com.amazonaws.transform.StaxUnmarshallerContext.nextEvent(StaxUnmarshallerContext.java:153)
... (rest of lines omitted)
I have double, triple, quadruple checked this XML for ‘invisible characters’ or non-UTF8 encoded characters, etc. I looked at it byte-by-byte in an array for byte-order-marks or something of that nature. Nothing; it passes every validation test I could throw at it. Even stranger, it happens if I use a Saxon-based parser as well — but ONLY on GAE, it always works fine in my local environment.
It makes it very hard to trace the code for problems when I can only run the debugger on an environment that works perfectly (I haven’t found any good way to remotely debug on GAE). Nevertheless, using the primitive means I have, I’ve tried a million approaches including:
- XML with and without the prolog
- With and without newlines
- With and without the «encoding=» attribute in the prolog
- Both newline styles
- With and without the chunking information present in the HTTP stream
And I’ve tried most of these in multiple combinations where it made sense they would interact — nothing! I’m at my wit’s end. Has anyone seen an issue like this before that can hopefully shed some light on it?
Thanks!
Discussions
Categories
I’m trying to compare an XML file to an XSLT generated file from that XML file, and when I run the the class as a JUnit Test, I get the following:
[Fatal Error] :1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException: Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at org.custommonkey.xmlunit.XMLUnit.buildDocument(XMLUnit.java:352)
at org.custommonkey.xmlunit.XMLUnit.buildDocument(XMLUnit.java:339)
at org.custommonkey.xmlunit.XMLUnit.buildControlDocument(XMLUnit.java:283)
at org.custommonkey.xmlunit.Diff.<init>(Diff.java:116)
at org.custommonkey.xmlunit.examples.MyXMLTestCase.testXSLTransformation(MyXMLTestCase.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Any ideas?
This discussion has been closed.
For example in Problems I have
Content is not allowed in prolog.
How to find out why its there(what eclipse plugin has put it there) and how to turn it off?
![]()
asked Mar 2, 2011 at 11:00
10
I had this problem too and it was, because i changed/saved the file in UltraEdit. After the save command, the file encoding changed and included characters, eclipse was not able to read.
You can open the file with the windows «Editor» tool and delete the characters, eclipse can not read. You will directly detect them.
answered Mar 2, 2011 at 11:25
![]()
Markus LausbergMarkus Lausberg
12.1k6 gold badges41 silver badges66 bronze badges
This sounds like an error with a xml file. Most of the time «Content is not allowed in prolog» means, that your XML file does not have the right format or even doesn’t start the right way.
answered Mar 2, 2011 at 11:04
ChrisChris
7,6158 gold badges49 silver badges99 bronze badges
1
«Content is not allowed in prolog» is the error thrown by Xerces when there’s something in an XML file or stream that precedes the <?xml?> declaration. There must be nothing before that, not even whitespace or a Byte-Order-Mark.
answered Mar 2, 2011 at 11:10
skaffmanskaffman
396k96 gold badges814 silver badges768 bronze badges
1
Double-click the message and it should take you to the file (and ideally location within the file) that is the source of the problem.
This specific error sounds like you’ve got a malformed XML file.
answered Mar 2, 2011 at 11:02
Joachim SauerJoachim Sauer
298k57 gold badges552 silver badges610 bronze badges
1
For example in Problems I have
Content is not allowed in prolog.
How to find out why its there(what eclipse plugin has put it there) and how to turn it off?
![]()
asked Mar 2, 2011 at 11:00
10
I had this problem too and it was, because i changed/saved the file in UltraEdit. After the save command, the file encoding changed and included characters, eclipse was not able to read.
You can open the file with the windows «Editor» tool and delete the characters, eclipse can not read. You will directly detect them.
answered Mar 2, 2011 at 11:25
![]()
Markus LausbergMarkus Lausberg
12.1k6 gold badges41 silver badges66 bronze badges
This sounds like an error with a xml file. Most of the time «Content is not allowed in prolog» means, that your XML file does not have the right format or even doesn’t start the right way.
answered Mar 2, 2011 at 11:04
ChrisChris
7,6158 gold badges49 silver badges99 bronze badges
1
«Content is not allowed in prolog» is the error thrown by Xerces when there’s something in an XML file or stream that precedes the <?xml?> declaration. There must be nothing before that, not even whitespace or a Byte-Order-Mark.
answered Mar 2, 2011 at 11:10
skaffmanskaffman
396k96 gold badges814 silver badges768 bronze badges
1
Double-click the message and it should take you to the file (and ideally location within the file) that is the source of the problem.
This specific error sounds like you’ve got a malformed XML file.
answered Mar 2, 2011 at 11:02
Joachim SauerJoachim Sauer
298k57 gold badges552 silver badges610 bronze badges
1
Table of Contents
- Sax Error Due to Invalid Text Before XML Declaration
- Byte Order Mark (BOM) At the Beginning of the XML File
- Passing a Non Existent File to Parser
- Different Encoding Formats Causing the Parser Error
- Conclusion
This article discusses the SAX Error – Content is not allowed in prolog.
The SAX parser is the XML parsing API that you can use to process the XML files. However, while using the SAX parser, you may encounter SAX error – content is not allowed in prolog.
Sax Error Due to Invalid Text Before XML Declaration
The XML files are structured using tags. Therefore, each XML file follows specified syntax.
If you place an unknown or invalid character before the XML declaration, you will get the aforementioned error while trying to parse the file using SAX error.
Let us see an example using the following XML file.
|
!<?xml version=«1.0» encoding=«utf-8»?> <person> <name> Mohtashim Nawaz </name> <age> 24 </age> <prof> Software Engineer </prof> </person> |
The code to parse the file is given below.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package java2blog; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class XmlParser { public static void main(String[] args) { SAXParserFactory f = SAXParserFactory.newInstance(); try { SAXParser parser = f.newSAXParser(); parser.parse(«sample.xml», new DefaultHandler()); } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } } } |
Output:
org.xml.sax.SAXParseException; systemId: file:///home/stark/eclipse-workspace-java/java2blog/sample.xml; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
The parser will raise the error. However, you can correct it by removing the extra characters and changing the file as given below.
|
<?xml version=«1.0» encoding=«utf-8»?> <person> <name> Mohtashim Nawaz </name> <age> 24 </age> <prof> Software Engineer </prof> </person> |
Observe that this XML file does not have (!) symbol at the beginning.
Byte Order Mark (BOM) At the Beginning of the XML File
The Byte Order Mark is a special unicode character that can indicate different things. The text editors may insert the BOM character at the beginning of the file automatically.
While parsing the XML file with the BOM character inserted in the beginning, you may encounter the SAX parser error if the file is parsed as stream of characters instead of stream of bytes.
However, it might not always be the case as in the latest version of Java the SAX parser can parse the BOM character correctly.
You can add or remove the Byte Order Mark character from the file using the code as well as manually in the text editor. Most of the text editors provide options to add or remove the BOM character.
Passing a Non Existent File to Parser
If you pass a file to parser that does not exist, you shall get the SAX parser error. The same can happen if you accidentally fail to provide the correct path.
So even if the file existed, if its path is not correct, you will eventually get the parser error.
Let us see an example.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package java2blog; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class XmlParser { public static void main(String[] args) { SAXParserFactory f = SAXParserFactory.newInstance(); try { SAXParser parser = f.newSAXParser(); parser.parse(«sample_unknown.xml», new DefaultHandler()); } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } } } |
The “sample_unknown.xml” file does not exist.
Output:
java.io.FileNotFoundException: /home/stark/eclipse-workspace-java/java2blog/sample_unknown.xml (No such file or directory)
However note that in this case the only error is the FileNotFoundException rather than parser error.
Different Encoding Formats Causing the Parser Error
The difference between the file encoding format and the encoding format you pass to the parser can cause the parser error.
For instance, if your file is encoded into UTF-8 encoding and you somehow pass the UTF-16 encoding to the parser, you will end up getting the parser error. Therefore, you should always check for the file encoding before parsing it.
Conclusion
This is all about the SAX error – content is not allowed in prolog. You can read more about SAX here.
Hope you have enjoyed reading the article. Stay tuned for more such articles. Happy Learning!
The other day, Dustin Chesterman asked me about an XML parsing error he was seeing. He was getting the «Content is not allowed in Prolog» XmlParse() error. I have blogged about this error before — it is an exception that is thrown when you try to parse XML that has data or white space prior to the encoding declaration or root node. This is often caused when an XML feed does not trim it’s return value. Usually, passing the content through ColdFusion’s Trim() method before calling XmlParse() does the trick; however, in Dustin’s case, Trim() didn’t seem to be helping.
He was working with Authorize.NET’s API, which returns XML responses. Let’s take a look at the call that was being made. For demonstration purposes, I am just going to call the Authorize.NET API without any data — this will error on their side, but will return a valid XML response:
<!---
Call Authorize.NET API. This will fail because we are not
passing any of the require information, but at least it will
return an XML result (error message) that we can then use.
--->
<cfhttp
method="get"
url="https://apitest.authorize.net/xml/v1/request.api"
result="objGet"
/>
<!--- Dump out the results. --->
<cfdump
var="#objGet#"
label="Authorize.NET Result"
/>
Running this code, we get the following CFDump output:
If you look at the FileContent key above, you will see that an XML document was returned. And, furthermore, from what you can see, it appears that the first piece of data returned is the encoding:
<?xml version="1.0" encoding="utf-8"?>
But, now, let’s try to parse this return value:
<!---
Parse Authorize.NET resposne into a ColdFusion XML object.
Be sure to Trim() the content to get rid of any white space.
--->
<cfset xmlResult = XmlParse(
Trim( objGet.FileContent )
) />
Notice that we are running the objGet.FileContent through ColdFusion’s Trim() method before parsing it. Usually, this would take care of any prolog data issues; however, running the above code, we get the following error:
An error occured while Parsing an XML document. Content is not allowed in prolog.
Clearly, there is data there that we are not seeing. Let’s loop over the first few characters of the response data to see what is going on:
<!--- Loop over first few characters of response. --->
<cfloop
index="intCharIndex"
from="1"
to="6"
step="1">
<!--- Get the character in question. --->
<cfset strChar = Mid(
Trim( objGet.FileContent ),
intCharIndex,
1
) />
<!--- Output char and Ascii values. --->
[#strChar#] - #Asc( strChar )#<br />
</cfloop>
After running the loop, we can see that there is, indeed, a leading character:
[] — 65279
[<] — 60
[?] — 63
[x] — 120
[m] — 109
[l] — 108
There is a mysterious leading character — 65279.
It turns out, this character is not just random data, it’s something called a Byte-Order-Mark and in an XML document, it is used to flag the encoding type of the XML. When you convert this byte into Hexadecimal, you get «FEFF». If you look on www.opentag.com, you will see that this byte signals a UTF-16 (big-endian) encoding:
- EFBBBF — UTF-8
- FEFF — UTF-16 (big-endian)
- FFFE — UTF-16 (little-endian)
- 0000FEFF — UTF-32 (big-endian)
- FFFE0000 — UTF-32 (little-endian)
- None of the above — UTF-8
Unfortunately, ColdFusion does not appreciate the use of this Byte-Order-Mark, or BOM. In order to get this kind of XML feed to play nicely with ColdFusion, we have to remove the BOM before we parse the document. Luckily, getting rid of this requires nothing more than a simple regular expression that strips out all characters before the first bracket:
<!---
Parse the return value into a ColdFusion XML
document. Remove the Byte-Order-Mark (BOM) by
stripping all pre-"<" characters.
--->
<cfset xmlResult = XmlParse(
REReplace( objGet.FileContent, "^[^<]*", "", "all" )
) />
<!--- Dump out XML resposne. --->
<cfdump
var="#xmlResult#"
label="Authorize.NET Clean Response"
/>
Running this, we get the following CFDump output:
As you can see, with the BOM character easily stripped out, we can now parse the XML data without issue. I don’t know much about BOM characters or how often they are used. I assume that since ColdFusion doesn’t play nicely with them that they are NOT common practice; but, I can’t really say for sure. Clearly they aren’t used everywhere or I would have come across this issue before. As such, I wouldn’t go around implementing this code for every XML feed you encounter — only for those that error out because of it.
Want to use code from this post?
Check out the license.
I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Content is not allowed in prolog is an error generally
emitted by the Java XML parsers when data is encountered before the <?xml...
declaration. You may inspect the document in a text editor and think
nothing is wrong, but you need to go down to the byte level to understand
the problem. You probably have a character encoding bug.
This code reproduces the problem:
import java.io.*;
import java.nio.charset.Charset;
import javax.xml.parsers.*;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ContentNotAllowedInProlog {
private static void parse(InputStream stream) throws SAXException,
ParserConfigurationException, IOException {
SAXParserFactory.newInstance().newSAXParser().parse(stream,
new DefaultHandler());
}
public static void main(String[] args) {
String[] encodings = { "UTF-8", "UTF-16", "ISO-8859-1" };
for (String actual : encodings) {
for (String declared : encodings) {
if (actual != declared) {
String xml = "<?xml version='1.0' encoding='" + declared
+ "'?><x/>";
byte[] encoded = xml.getBytes(Charset.forName(actual));
try {
parse(new ByteArrayInputStream(encoded));
System.out.println("HIDDEN ERROR! actual:" + actual + " " + xml);
} catch (Exception e) {
System.out.println(e.getMessage() + " actual:" + actual + " xml:"
+ xml);
}
}
}
}
}
}
The output:
Content is not allowed in prolog. actual:UTF-8 xml:<?xml version='1.0' encoding='UTF-16'?><x/> HIDDEN ERROR! actual:UTF-8 <?xml version='1.0' encoding='ISO-8859-1'?><x/> Content is not allowed in prolog. actual:UTF-16 xml:<?xml version='1.0' encoding='UTF-8'?><x/> Content is not allowed in prolog. actual:UTF-16 xml:<?xml version='1.0' encoding='ISO-8859-1'?><x/> HIDDEN ERROR! actual:ISO-8859-1 <?xml version='1.0' encoding='UTF-8'?><x/> Content is not allowed in prolog. actual:ISO-8859-1 xml:<?xml version='1.0' encoding='UTF-16'?><x/>
This code also highlights another, more insidious
character encoding issue — when we can accidentally encode with one
encoding thinking it is another and everything seems to work.
When you inspect the data in a hex editor problems become more
apparent.
A valid UTF-16 form:
FF FE 3C 00 3F 00 78 00 6D 00 6C 00 20 00 76 00 __<_?_x_m_l_ _v_ 65 00 72 00 73 00 69 00 6F 00 6E 00 3D 00 27 00 e_r_s_i_o_n_=_'_ 31 00 2E 00 30 00 27 00 20 00 65 00 6E 00 63 00 1_._0_'_ _e_n_c_ 6F 00 64 00 69 00 6E 00 67 00 3D 00 27 00 55 00 o_d_i_n_g_=_'_U_ 54 00 46 00 2D 00 31 00 36 00 27 00 3F 00 3E 00 T_F_-_1_6_'_?_>_ 3C 00 78 00 2F 00 3E 00 <_x_/_>_
Note: exact UTF-16 byte forms vary — big-endian,
little-endian, with or without a byte-order-mark. This one is
little-endian with a BOM.
An XML document that declares itself as UTF-16 but is really
UTF-8:
EF BB BF 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E ___<?xml version 3D 27 31 2E 30 27 20 65 6E 63 6F 64 69 6E 67 3D ='1.0' encoding= 27 55 54 46 2D 31 36 27 3F 3E 3C 78 2F 3E 'UTF-16'?><x />
Note: UTF-8 XML documents can come with or without a
byte-order-mark. This one includes a BOM.
XML, Java and Encodings
- XML
1.0: Autodetection of Character Encodings (Non-Normative) - Java:
a rough guide to character encoding
The code was written and tested against Sun’s win32 Java 1.6.0_17
which uses a version of the Apache Xerces parser internally.
Content Is Not Allowed In Prolog is an error when the documents contains invisible characters or characters before the XML header.
No Beginning Tag:Content Is Not Allowed In Prolog
![]() |
| Content Is Not Allowed In Prolog |
Prolog error is also shown when yhe XML header do not have a beginning tag “<” before thePseudo Attribute, firstquestion mark in the XML header as shown in the image. If the document in not proper UTF and there are invisible characters between the XML header, a prolog error can be shown.
AOnline Syntax Error Parsing XML Validator Toolcan be used to check the parsing errors and find invisible characters.
To Solve this Prolog error;
Copy either of the below code and replace it with the existing XML header;
<?xml version=”1.0″ encoding=”UTF-16″ standalone=”no”?>
or
<?xml version=”1.0″ encoding=”UTF-8″ ?>
Byte Order Markers
Byte order markers could be in the buffer. Before passing the buffer to the Parser do this.
String xml = “<?xml …”;
xml = xml.trim().replaceFirst(“^([W]+)<“,”<“);
Clearing the white spaces between characters also helps in sorting out the issue. If the XML header has white space before it, you may encounter another error:The processing instruction target matching “[xX][mM][lL]” is not allowed as shown in the below image.
![]() |
| The processing instruction target matching “[xX][mM][lL]” is not allowed |
Characters Before XML header:Content Is Not Allowed In Prolog
If there are visible or invisible characters before the XML header, the error Content Is Not Allowed In Prolog will pop up. It
![]() |
|
Characters Before XML header |
As shown in the image, a character “x” was put before XML header, and error was shown. Even if there an extra small dot “.” in the beginning of XML element, the error will pop up.
Use Notepad++ to copy and paste all the codes and display all the characters by selecting Encoding “UTF-8 without BOM”. Solution is to remove all the whites pace and characters before the XML header.
Please note! This page describes the nature of the error using a hypothetical example and not the erroneous data of the input test file. You should however be able to apply this information to your error case.
General description of the error:
The format of the error message: Content is not allowed in prolog.
Error description in schema standard: http://www.w3.org/TR/2004/REC-xml-20040204/#NT-Misc
More information about prolog here.
Meaning of the error: Information not considered as valid prolog is given at the beginning of the file. Prolog is optional and it has to be present before the root tag of the file. If prolog is given, it has to contain version attribute, in addition also encoding and standalone attributes can be given.
Possible causes for this error:
- Prolog has incorrect content
- Invalid content given after prolog. Either root tag or comment should be present
- Invalid content given before prolog. Prolog should be the first thing in the file if given.
Example
?<xml version=»1.0″ encoding=»UTF-8″ standalone=»no»?>
Error message: Content is not allowed in prolog.
How to fix: Modify prolog so that attributes version, encoding and optionally standalone are given. Prolog is to be inserted inside opening and closing tags «<«, «>» and inside question marks «?», with attribute names separated by spaces.
<?xml version=»1.0″ encoding=»UTF-8″ standalone=»no»?>

![The processing instruction target matching "[xX][mM][lL]" is not allowed The processing instruction target matching "[xX][mM][lL]" is not allowed](https://www.shipmethis.com/wp-content/uploads/2021/07/The2Bprocessing2Binstruction2Btarget2Bmatching.png)
