Exception: Content is not allowed in prolog-搜集

来源:互联网 发布:2017网络兼职诈骗案例 编辑:程序博客网 时间:2024/06/04 18:07

用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:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN "
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " 
>
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head >
<title >The Symbol Grounding Problem </title >
</head >
<body >
</body >
</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 的解析器。

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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魅族手机变成英文怎么办 员工失去了工作乐趣怎么办 古墓丽影9出bug怎么办 气炉子打不着火怎么办 下面人员不参加公司拓展怎么办 重点班的差生怎么办 江苏考生选修考d怎么办 眼睛里有虫子该怎么办 屋里毛絮特别多怎么办 信用卡人员核核查单位地址怎么办 红米note电源键失灵怎么办 红米note4x跳屏怎么办 红米开机键坏了怎么办 红米note2开关键不灵敏怎么办 大学生论文检测系统忘记密码怎么办 快手实验室没有k歌怎么办 皮肤一到冬天就会起皮脱落怎么办 嘴上的皮撕破了怎么办 身体皮肤干燥起皮怎么办 把公司数据库弄宕机了怎么办 地铁上临时变站怎么办 大学实践课挂了怎么办 军训的人长得矮怎么办 军训脸被晒黑了怎么办 晚上吃了油炸的怎么办 楼梯又陡又窄怎么办 小孩不认真做作业怎么办 水厂出水水质不达标怎么办 野猪铁夹子夹到怎么办 燃气灶开孔尺寸大了怎么办 苹果电脑视频显示缺少插件怎么办 安装拼多多显示校验失败怎么办 中央庭黑盒被抢怎么办 lab值a值偏高怎么办 橱房管道堵死了怎么办 微信被别的手机同步登录怎么办 计算机机房空调坏了怎么办 被蝎子蛰了怎么办怎么处理? 月嫂走了孩子闹怎么办 转业军人自建房有困难怎么办 转业军人没配偶想异地安置怎么办