关于SAXparseException:prolog中不允许使用内容

时间:2010-09-08 07:40:00

标签: java xml

我正在使用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

1 个答案:

答案 0 :(得分:4)

检查此链接

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()dost不够,因为它只修剪有限的空白字符。