4

I having a strange problem when I'm trying to produce an XML file with PHP. The problems is that, a strange question mark appears at the end of the source code. And I get the error: "Extra content at the end of the document"

I'm running this script:

<?php
header("Content-Type: application/xml");
echo "<?xml version=\"1.0\"?>";
?>

<Module>
      <ModulePrefs title="Ngram Extractor"
        author="interedition team"
       description="Ngram Extractor"
        scrolling="true"/>
        <Content type="html">
        Test
        </Content>
</Module>​

When I open this is a browser, I get the state error and the source code looks like this. Notice the strange question mark at the end.

 <?xml version="1.0"?>
<Module>
      <ModulePrefs title="Ngram Extractor"
        author="interedition team"
       description="Ngram Extractor"
        scrolling="true"/>
        <Content type="html">
        Test
        </Content>
</Module>?

Please help.

Jeff
  • 3,943
  • 8
  • 45
  • 68
  • spurious echo somewhere in your code, or text that isn't enclosed in tags – Mark Baker Jan 13 '12 at 11:52
  • 1
    The strange character is probably present att he end of your source file, or generated because you're not ending the XML in the source file with a line feed. – Joachim Isaksson Jan 13 '12 at 11:52
  • how do I end the XML with a line feed? – Jeff Jan 13 '12 at 12:02
  • in general, it doesn't seem like the browser is even recognizing this an xml document? I'm not sure what I'm doing wrong. – Jeff Jan 13 '12 at 12:06
  • @Jpsy well that's good to know and also makes my problem even more strange :) – Jeff Jan 13 '12 at 12:12
  • I have removed my comment and posted an answer below. My first test did not show any question marks or errors, but my browser (Chrome & IE9) also did not recognize the output as XML. This brought me onto the right track. – Jpsy Jan 13 '12 at 12:34

3 Answers3

7

You have a zero width space character at the end of your file - UTF-8 code E2 80 8B. It is placed right after </Module>. Have a look at your file in hex mode. This extra character prevents your browser from recognizing this as valid XML and - depending on the browser in use - shows up as a question mark or does not show up at all.

Remove that extra character and you will be fine.

Jpsy
  • 20,077
  • 7
  • 118
  • 115
0

May be you use UTF-8 with BOM encoding. Check it

Ramil Amerzyanov
  • 1,301
  • 12
  • 26
0

I don't really have answer, accept that my editor wasn't showing the question mark. But one I opened the file in notepad2, lo and behold, the question mark was there. Very strange. Thanks for your help.

Jeff
  • 3,943
  • 8
  • 45
  • 68
  • As said in my response above: This question mark was an invisible UTF-8 character. It even made it into the copy of your code here at StackOverflow - although it cannot be seen (not even in HTML code view). But when copied into a Hex editor the UTF-8 code E2 80 8B shows up at the end of your code. – Jpsy Jan 13 '12 at 12:45