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).
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.
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