There is one other case where you will get this error.  That’s where you include a prolog that declared the data as UTF-16, but the actual data written was NOT truly UTF-16.  Here is a snippet I use to write data out:

 

                                    new File(filePath).delete();

 

                                    // open file

                                    FileOutputStream os = new FileOutputStream(new File(filePath));

 

                                    // write content

                                    String strXML = getXML(doc, bIndent, strEncoding, bOmitXMLDeclaration);

                                    os.write(strXML.getBytes(strEncoding));

 

                                    // close file

                                    os.flush();

                                    os.close();

 

The string strXML is stored in internal Java string format, which is not exactly UTF-16.  Most mechanisms to write string data to a file result in the data being written using the current code-page, which is likely neither UTF-8 nor any variation of UTF-16.  (You can use the old DOS DEBUG command to see if you actually have 16-byte encoding or not.)  Writing a bytestream that has been selected in the appropriate format seems to solve the problem.  You will have to select either Little Endian or Big Endian UTF-16 variations (“UTF-16LE” or “UTF-16BE” as I recall it) for strEncoding.  I can’t remember if this generates the appropriate byte order mark (I believe it does), but most parsers will check the first several bytes in the absence of a BOM, and if they see 0, ‘<’ or ‘<’ 0, they will figure it out for you.  However, if they see an encoding in the prolog that doesn’t match the actual file, the parser will then assumed that what they thought was a prolog was actually bogus leading bytes, and hence the message you are getting.

 

In short, there’s more than one way to get this error message.


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Monday, October 30, 2006 10:12 AM
To: j-users@xerces.apache.org
Subject: Re: Reading UTF-16: Content is not allowed in prolog

 

[Fatal Error] output.xml:1:40: Content is not allowed in prolog.

You have something other than the Byte Order Mark, the XML Declaration, Processing Instructions, or whitespace before the document's root element. Fix the file so it's well-formed XML.


______________________________________
"... Three things see no end: A loop with exit code done wrong,
A semaphore untested, And the change that comes along. ..."
-- "Threes" Rev 1.1 - Duane Elms / Leslie Fish (http://www.ovff.org/pegasus/songs/threes-rev-11.html)

Reply via email to