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

最佳答案

检查此链接

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()仅修剪有限的空白字符,因此还不够。

09-28 11:40