This error is generated when characters are located before the XML document type declaration.
The very first line and very first characters of an XML document should look like this:
<?xml version="1.0" encoding="utf-8"?>
When characters (any character) is placed before the type declaration it causes this error. For example, this code will cause the error:
helloworld<?xml version="1.0" encoding="utf-8"?>
Even a simple space character (called a whitespace character) can cause this error!
<?xml version="1.0" encoding="utf-8"?>
Notice that the first character in the code above contains a space. White space characters include spaces, line breaks, tabs and so on.
Some text programs use characters that are not allowed in XML or certain sections of it. For example, “fancy quotation marks”, or curly or curvy quotes both single and double, used on websites can cause trouble. If you copy code from an example on a webpage and the quotation marks being used are the curly types it may be causing this error. Check if your XML type declaration contains any of these.
For example, look closely at the quotes used in the code below.
<?xml version=“1.0” encoding=“utf-8”?>
To fix this scenario retype all fancy quotes with regular quotes.
This can also be caused by specifying the encoding in uppercase letters as shown below:
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="utf-8"?>
Having the incorrect UTF type can cause this as well:
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-16"?>
In cases where you are dynamically generating XML with PHP you may inadvertently be sending a whitespace character before you have a chance to send your XML content.
In the code below there is a line break before the first php tag. Since PHP sends any content to the client outside of php tags that linebreak will be sent before the XML is output. Also, after the last php tag there is a space character. This can cause other errors as well!
<?php> // output XML </php>
Remove the characters before and after the php tags.
Sending headers back before the XML content is sent can also cause this error. This happens if headers are not setup correctly or content is sent before the headers are created.
<?php>
// pseudo code
// notice incorrect header set to image mime type
write output->headers("image");
echo xml->toString();
</php>
Even if you don’t see any characters before the beginning XML tag there may STILL be characters there! In the exchange of textual information there is sometimes metadata characters that describe the type of encoding of the text being transmitted. These are known as Byte Order Marker (BOM for short).
From Wikipedia,
The byte order mark (BOM) is a Unicode character used to signal the endianness (byte order) of a text file or stream. Its code point is U+FEFF. BOM use is optional, and, if used, should appear at the start of the text stream. Beyond its specific use as a byte-order indicator, the BOM character may also indicate which of the several Unicode representations the text is encoded in.[1]
Because Unicode can be encoded as 16-bit or 32-bit integers, a computer receiving Unicode text from arbitrary sources needs to know which byte order the integers are encoded in. The BOM gives the producer of the text a way to describe the text stream’s endianness to the consumer of the text without requiring some contract or metadata outside of the text stream itself. Once the receiving computer has consumed the text stream, it presumably processes the characters in its own native byte order and no longer needs the BOM. Hence the need for a BOM arises in the context of text interchange, rather than in normal text processing within a closed environment.
Disclaimer: The following information is to the best of my understanding.
Since this is metadata you will NOT see these characters in a text editor. They are hidden by the text editor before it is displayed to you.
Normally all of this happens behind the scenes but if you are reading this something went wrong.
If that is the problem then you may be able to remove these characters simply by opening the file and hitting the backspace a few times. Sometimes this will not work. In these cases you can create a new text file with software that doesn’t add this information. IE Notepad on Windows does in some cases and does not let you change it later. In this case you may need to re-encode your file.* Note there may be an easier way to fix this (changing the encoding type in the XML type declaration?).
You can check the encoding of your file using an program such as Notepad2. When you open an file it will show you the type of encoding in the File menu item. This program also allows you to re-encode it. See screenshot below.
If you are creating XML dynamically you can use the XML Validator to check your XML and get detailed error messages. It’s best used in Firefox and Chrome. You may get the best results by running your XML through both since it is processed by the browsers built in parsers. This is version 1.0. The next version may check for the byte order marker. Hopefully by the time you read this it will be included.
Please reply in the comments below if this helped you or not

solution was much more helpful.. Thank you for that..
10x this helped me diagnose an strange complaint in eclipse ant editor. Fixed with TC 7.0pb1 compare editor.
thank you, this helped me , i had this error
I m getting the same problem, but cant figure out. I cant see any character before
Even if you can’t see any characters there may be characters there.
Suggestions:
- Copy the text in your file and paste it into Notepad. Notepad strips or converts non standard codes. It is not fool proof but seems to work in most cases.
- Create a new file using your standard process. Copy the contents of your original file and paste them into the new file. The content being everything minus the beginning and end tags.
I was receiving the same “content not allowed in prolog”. I changed the encoding=”UTF-8″ to encoding=”utf-8″ and it works.
@Mike L, I was getting this error and resolved by making the encoding value lowercase. Thanks for posting!
I still have this problem, I don’t know what to do . I have tried to change the UTF to utf ,and I tried to copy the hole text to the notepad, and I didn’t see anything before:
asdf
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
I create a new project and at begining I have this error.. .Content is not allowed in prolog.
Please anyone … help !!!!!!
I ment before “”
I am trying to parse an XML file that contains non-tagged items. Is there anyway to parse this file still? I found online it said to basically scan the whole file and toss the things that aren’t needed.
I have changed the encoding=”UTF-8″ to encoding=”utf-8″ and it works. Thanks Mike L.
I was getting same error but after rewriting following line it works, Thanks.
When I run the build.xml on my RAD 7.0, it works fine, but when I commit to CVS and if I run from there I am getting “Content is not allowed in Prolog”. Can any one help me resolve this, Quick response is highly appreciated. Thanks in advance.
I was getting the same problem and changing utf-8 to utf-16 fixed it.
FWIW, ran into an issue with this recently, found that changing the text file’s format to ANSI fixed it; the prolog still states utf-8. I don’t know what parser the consuming application is using, but it’s Java of some flavor.
This helped me. Thanks!! Keep up the good thing..
thank you for posting this. i know it’s been over 2 years since you posted, but you saved me a morning of frustration.
Rhonda
Hi, I get a error on the web site that says:
“Fatal Error: Content is not allowed in prolog.FieldParser: DTD Validation!org.xml.sax.SAXParseException: Content is not allowed in prolog.”
What that means? I don´t know how to fix this error.
Thanks for the attention.
Luis.
Great tip. In my case retyping the header worked. I can only imagine it was an “invisible” Windows/Linux or Unicode character issue.
Thank you it helped me!
You have to watch for BOM. If you have an UTF-8 file with BOM it causes problems. If you have some nice editor like Kate just unselect the option “Add Byte Order Mark”
Here’s an easy to use XML Validator. The version I have running locally has a BOM test. http://www.judahfrangipane.com/utils/xmlvalidator/XMLValidator.html
I did have this error also when reading xml using java, same error but notepad did not help me. Instead I use a simple system.out.println(xml) to display the file again. Then I saw some funny character in front of the xml. I manually copy it from the java console and recopy it to the text pad. Resave and run again. It works after that.
for..the above problem..
change
to
To include code select it in the comment text box and click one of the links directly above the text box that corresponds to the name of the language you are posting.
Hello,
I obtain the same fatal error when running my aplication in NetBeans. This IDE checks that the XML document is well-formed. After reading this post, I opened the XML document with UltraEdit to rewrite the first line and deleting any BOM. I also wrote utf instead of UTF, used the XML Validator… but I still obtaining this error. What am I doing wrong?
Thanks a lot.
Hi Rocio,
I’ve updated the article. Please reread it and see if any of the updated information helps solve it.
You will receive this error if you accidentally point your path to a directory instead of an XML file (oops).
Exactly, it was the problem… A really stupid error that took me too much time to see!!! I resolved it some weeks ago but I was so busy and stressed that I forgot to post it here. Thank you all for answer my question and for your help. Bye
I tried all the above. I do not get this error in general but I get this error when i add jbossts-common.jar in classpath.
Please let me know the solution.
TIA
thanks a ton…i wasted 2 hrs solving it!!!!thanks…
My WSDL file starts with
But it still works fine with upper-case UTF
My WSDL file starts with
but it still works
I/O error reading OEBPS/content.opf – what s mean by this error. can anybody say this?
Much appreciated answer
this really helped to understand the problem and find a solution
I solved this error deleting this auto-generated lines by Eclipse in the wsdl file.
When I created the wsdl with the Eclipse wizard, it puts me 3 lines like this, and apparently, it produced the error.