Exception: Content is not allowed in prolog-搜集

來源:互聯網
上載者:User

用webwork驗證時老發生錯誤,提示Content is not allowed in prolog,由於是解析xml檔案出錯,找到相應的檔案一看,發現開頭有幾行漢字說明忘注釋掉了,我把它們注釋掉,問題得到解決,由於以前沒遇到這種問題,於是到網上找到如下資料:
DOM4j 讀取xml檔案可能會抱錯:Content is not allowed in prolog異常的原因
該xml是UTF-8編碼的,如果該檔案通過Ultraedit編輯後,會在無BOM頭的UTF-8檔案中加入BOM,但是DOM4j不認這個BOM(dom4j1.3),解決的辦法可以通過升級dom4j到1.6解決www.dom4j.org
什麼是BOM?http://www.unicode.org/faq/utf_bom.html#22

Unicode規範中有一個BOM的概念。BOM——Byte Order Mark,就是位元組序標記。在這裡找到一段關於BOM的說明:

在UCS 編碼中有一個叫做"ZERO WIDTH NO-BREAK SPACE"的字元,它的編碼是FEFF。而FFFE在UCS中是不存在的字元,所以不應該出現在實際傳輸中。UCS規範建議我們在傳輸位元組流前,先傳輸字元"ZERO WIDTH NO-BREAK SPACE"。這樣如果接收者收到FEFF,就表明這個位元組流是Big-Endian的;如果收到FFFE,就表明這個位元組流是Little-Endian的。因此字元"ZERO WIDTH NO-BREAK SPACE"又被稱作BOM。

UTF-8不需要BOM來表明位元組順序,但可以用BOM來表明編碼方式。字元"ZERO WIDTH NO-BREAK SPACE"的UTF-8編碼是EF BB BF。所以如果接收者收到以EF BB BF開頭的位元組流,就知道這是UTF-8編碼了。

Windows就是使用BOM來標記文字檔的編碼方式的。

另外unicode網站的FAQ-BOM詳細介紹了BOM。官方的自然權威,不過是英文的,看起來比較費勁

===============================================

看到一篇關於在eclipse中使用ant構建java項目的文章,就按照文章中的說明建立了一個項目,並建立了classes,dist,doc,lib幾個檔案夾和build.xml檔案。其中build.xml檔案的內容也是直接複製的,由於是從網頁上直接複製的,裡面“<”“>”都是中文格式的,我就把這些給替換了一下。
         但當選中項目,然後選擇“Project”,“Properties”,“Builders”,“New…”,選擇“Ant Build”:載入“Bulidfile”時提示“Content is not allowed in prolog”錯誤。    
         我就去Google上轉了一圈,還有點收穫,看了一篇小文章,覺得可能是build.xml檔案格式的錯誤。

In SGML and XML, a document is composed of two sequential parts,
the prolog and the instance. You can see this in an HTML example:

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN "
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
3 <html xmlns= "http://www.w3.org/1999/xhtml " >
4 <head >
5 <title >The Symbol Grounding Problem </title >
6 </head >
7 <body >
8 </body >
9 </html >

In this example, the prolog is lines 1-2, the instance begins on
line 3. The prolog includes the DOCTYPE declaration, the external
subset (called the DTD), and the internal subset (which you seldom
see but it 's legal). The document instance includes the document
element (in this case <html > and all of its descendent content).

You generally don 't want to see the prolog, and you generally don 't
want to store it. The DOCTYPE declaration provides references to
DTD, which is instantiated as part of the process of validating the
document. You may want to store the reference(s), but you wouldn 't
want to store the DTD each time you store the document, as that
would be a real waste (the DTD is often bigger than the document).

It sounds like your well-formed and valid document isn 't being
considered as such by the XML processor. The error message indicates
that there is content (i.e., either elements or character data) in
the part of the document considered as the prolog. You may be missing
the last " > " on line 2 above, as that would normally be the beginning
of the internal subset. If it found " <html " (or something similar),
you might get that error.
果然,有兩個“!”是中文狀態,改過來就ok 了。-----------------------------------------------------------------------------------------------------------------------------------------------

在用UltraEdit 編寫hibernate的對應檔時,發現UltraEdit會自動向UTF8編碼的檔案的最開頭,加入一個特殊字元。這在UltraEdit中是看不到的,在別的編輯器中可以看到。當使用dom4j解析檔案時,出現content is not allowed in prolog錯誤。用別的編輯器去掉這個字元,錯誤就可以修正。

應該換一個 xml 解析器。 utf-8 檔案頭是 unicode 標準的,在 xml 標準中也提到過。有一部分 java 寫的 xml 解析器比較爛,不認識這種檔案頭。但是比較好的解析器都認識。比如 apache 的解析器。

------------------------------------------------------------------------------------------------------------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.