Exception in thread "main" org.xml.sax.SAXParseException: Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
But... the good thing is that it is not hard to fix.
There are two cases when this problem might occur-
1. Your XML has some strange character in the beginning, even before the the angle bracket "<", called a Byte Order Mark. More info - http://en.wikipedia.org/wiki/Byte_order_mark
Actually, that's not a strange character :)
2. Your XML has an encoding other than what is specified by the encoding="encoding".
There is quick remedy for both of these problems.
Solution for the first problem-
All you need to do is t remove the strange character from the text representing the xml contents. You can do it either by taking a substring
String noBOMXmlString = xmlString.substring( xmlString.indexOf( '<' ), xmlString.length() );
or for a better control you may use regular expressions to find and remove this character.
and thats all that is need to get rid of it. :)...Simple..isn't it?
Solution for the second problem-
This case applies when you are writing the xml yourself and then later reading it. What goes wrong is that when writing th xml file to the disk if you do not set the encoding then the platforms default encoding is taken which might be different than what is specified in the encoding="encoding". For instance, the xml is-
xml version="1.0" encoding="UTF-16"......
an you are writing the xml on a platform say Windows XP. The file is written in UTF-8. So, later when you read it, the mismatch of the encoding specified and the actual encoding don't match and you the "Content is not allowed in prolog." error. To fix this, all you need to do is to set the encoding type before writing the xml to the disk.
final File outFile = new File( outFileName ) );
// Prepare the DOM document for writing
final Source source = new DOMSource(doc);
final Result result = new StreamResult(new FileOutputStream(outFile));
// Write the DOM document to the file
final Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
//set the encoding
xformer.setOutputProperty ( OutputKeys.ENCODING , encoding ) ;
xformer.transform(source, result);
the encoding you can find out from the line - xml version="1.0" encoding="UTF-16".....
Hope this helps...
Sanjeev Gour.

5 comments:
Congrats Sanjeev... U just became the author of most horrible blog I have ever read... I hope no one can ever break this record!!! truely a "Blog Atyachar!!!"
i was facing the same problem and it helped me. thanks
rs
My problem get resolved by following your blog. Thanks keep posting :)
I was having the same problem since long time.....it was appearing randomly for me.......perhaps some xmls had the right encoding. Thanks for the tips.
Hi,
You mentioned "or for a better control you may use regular expressions to find and remove this character."
Could you tell me how to do this? I am new to regular expression.
Thanks in advance.
Jeetendra Prasad
Post a Comment