【问题标题】:about SAXparseException: content is not allowed in prolog关于 SAXparseException: prolog 中不允许内容
【发布时间】:2011-04-09 14:18:53
【问题描述】:

我正在使用 glassfish 服务器,但不断出现以下错误:

Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
        at com.sun.enterprise.deployment.io.DeploymentDescriptorFile.read(DeploymentDescriptorFile.java:304)
        at com.sun.enterprise.deployment.io.DeploymentDescriptorFile.read(DeploymentDescriptorFile.java:226)
        at com.sun.enterprise.deployment.archivist.Archivist.readStandardDeploymentDescriptor(Archivist.java:480)
        at com.sun.enterprise.deployment.archivist.Archivist.readDeploymentDescriptors(Archivist.java:305)
        at com.sun.enterprise.deployment.archivist.Archivist.open(Archivist.java:213)
        at com.sun.enterprise.deployment.archivist.ApplicationArchivist.openArchive(ApplicationArchivist.java:813)
        at com.sun.enterprise.instance.WebModulesManager.getDescriptor(WebModulesManager.java:395)
        ... 65 more

【问题讨论】:

标签: java xml


【解决方案1】:

查看此链接

http://mark.koli.ch/2009/02/resolving-orgxmlsaxsaxparseexception-content-is-not-allowed-in-prolog.html

简而言之,一些 XML 文件在前面(<?xml ...?> 之前)包含三字节模式(0xEF 0xBB 0xBF),这是 UTF-8 字节顺序标记。 java 的默认 XML 解析器无法处理这种情况。

快速而肮脏的解决方案是删除 XML 文件前面的空白:

String xml = "<?xml ...";
xml = xml.replaceFirst("^([\\W]+)<","<");

注意 String.trim() 不够,因为它只修剪有限的空白字符。

【讨论】:

  • 很好,我刚刚被一系列文件的 UTF8 Bom 烧毁了。
  • 更好的解决方案是使用Spring框架提供的BOMInputStream
  • 太棒了!解决了。​​
猜你喜欢
  • 2011-06-01
  • 2011-07-23
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
相关资源
最近更新 更多