Why can some XML output not be parsed?

Sometimes, a script may generate incorrect XML output, which then causes another script (or perhaps a web browser) to give an error. For example, you may see an error like this:

This page contains the following errors:
error on line 2 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

This can be caused by having one or more extra blank lines at the start of an XML document. There should be no blank lines before the opening <?xml> tag.

This problem often happens because there are extra blank lines at the top or bottom of a PHP file.

One way to find blank lines is to run this shell command, which will show you every PHP file that begin or ends with blank lines:

for f in `find . -name "*.php"`; do
  perl -p -e 's/[\r\n]/_IS_CRLF_/g;' $f | egrep -q '^(_IS_CRLF_){4,}<\?|\?>(_IS_CRLF_){4,}$' && echo $f;
done

Another way for technically advanced users to find the offending PHP file is to use strace:

  1. Run the file which contains the problem with: strace php index.php 2>&1 | less
  2. Search in the "less" output for an empty line. To do this, press: /^$ and press Enter. If that doesn’t find anything, you can sometimes find it by searching for “write” that starts with a carriage return or line feed, as /^write\(1, "\\[rn]".)
  3. When you find the blank line, look up a few lines to see the last file that was opened.

In many cases, the last file that was opened will be the one that has the problem. If not, the last file that was opened was probably "required" or "included" from another PHP file that does have the problem.

If you're still having trouble finding the offending file and the problem just recently started occurring, try searching for recently modified files.

We know the above information is pretty technical; our customers can contact us if you'd like us to look into it for you.