If you wish to create an XML file and then parse it to use the containing information for some other logic you should keep in mind a small, but annoying “feature”.
Error :
-> org.xml.sax.SAXParseException: Content is not allowed in prolog.
Why:
-> Java known bug:
A Utf-8 stream can optionally beign with a byte order mark (see, for example http://www.unicode.org.unicode/faq/utf_bom.html). This is the character FEFF, which is represented as EF BB BF in utf-8. Java’s utf-8 encoding does not recognize this character as a BOM, though; the result of reading such a stream is a set of characters beginning with FEFF.
-> So, when the XML is generated, before the tag “<?xml…” there will be 3(or more?) hexadecimal characters that will make the parsing logic fail with honors. It looks something like this, although the characters I am using to demonstrate are not the exact ones: “Þ?u<?…”. This is not a valid XML Syntax(having other chars before the ‘<‘) and it will, of course fail when you want to parse it.
One Solution:
– read the XML and convert it into one string(ex. String temp), extract those characters before the “<?” and then create and array of strings resulting from spliting the temp variable. Walking through this array, append each string to a StringBuffer and then you are free to carry on with your logic. It can look something like this:
... <span style="color: #003399;">String</span> temp <span style="color: #339933;">=</span> <span style="color: #0000ff;">""</span><span style="color: #339933;">;</span> readBuffer.<span style="color: #006633;">append</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"<?"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #003399;">String</span><span style="color: #009900;">[</span><span style="color: #009900;">]</span> xmlData <span style="color: #339933;">=</span> temp.<span style="color: #006633;">split</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&</span>lt<span style="color: #339933;">;</span> xmlData.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> readBuffer.<span style="color: #006633;">append</span><span style="color: #009900;">(</span>xmlData<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> ... ...
<span style="color: #003399;">String</span> temp <span style="color: #339933;">=</span> <span style="color: #0000ff;">""</span><span style="color: #339933;">;</span>
readBuffer.<span style="color: #006633;">append</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"<?"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span><span style="color: #009900;">[</span><span style="color: #009900;">]</span> xmlData <span style="color: #339933;">=</span> temp.<span style="color: #006633;">split</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&</span>lt<span style="color: #339933;">;</span> xmlData.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
readBuffer.<span style="color: #006633;">append</span><span style="color: #009900;">(</span>xmlData<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
... ... String temp = ""; readBuffer.append("<?"); String[] xmlData = temp.split("\n"); for (int i = 0; i < xmlData.length; i++) { readBuffer.append(xmlData[i]); } ... |
Other solutions can be found here, at this Sun Forum page .
If you have a more elegant solution for this I would be glad to know, so feel free to leave a comment with it.
Enjoy