org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog

用sax读xml文件时常会出现这个异常,一般上网可以看到的原因是:

1.BOM

在java中测试inputstream中是否有bom可以用apache commons IO 的org.apache.commons.io.input.BOMInputStream,如果你的项目有引入IO的情况下

BOM的基础知识可以参考:http://www.unicode.org/faq/utf_bom.html

也贴一个过滤BOM的小方法

private static InputStream checkForUtf8BOMAndDiscardIfAny(InputStream inputStream) throws IOException {
    PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);
    byte[] bom = new byte[3];
    if (pushbackInputStream.read(bom) != -1) {
        if (!(bom[0] == (byte) 0xEF && bom[1] == (byte) 0xBB && bom[2] == (byte) 0xBF)) {
            pushbackInputStream.unread(bom);
        }
    }
    return pushbackInputStream;
}

2.非良构的xml内容,例如在<?xml前出现非法的字符

可以采用正则从<?xml开始截取内容

3.今天我发现的是因为:Accept-Encoding启用了压缩传输,可以试着把Accept-Encoding设成:identity,同时也要注意是否采用Chunked Transfer Encoding(分段传输)

后话,有人说直接把流传给sax parser可以过滤掉。当然我用的sax,jaxp发现不会过滤掉。文档测试的环境:java 7 + xerces sax。全部异常栈

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
	at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
	at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
	at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
	at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source)
时间: 2024-12-28 09:18:57

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog的相关文章

Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in

1.错误描述 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from file [E:\Ecl

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。

二月 25, 2016 9:24:24 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SpringMvc-1' did not find a matching property. 二月 25,

Bug解决方案:org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 8; 不允许有匹配 &quot;[xX][mM][lL]&quot; 的处理指令目标

十月 17, 2016 10:14:30 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org[email protected]2e0fa5d3: startup date [Mon Oct 17 22:14:30 CST 2016]; root of context hierarchy十月 17, 2016 10:14:30 下午 org.spring

Caused by: org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 84; 对实体 &quot;characterEncoding&quot;

1.错误描述 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 30 in XML document from class path

Caused by: org.xml.sax.SAXParseException; lineNumber: 28; columnNumber: 81;

1.错误描述 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 28 in XML document from class path

spring-bean 版本的问题(报错:org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 75;)

当XML中配置的xsd是4.0,而引用的包是4以下的spring-bean.jar时,当服务器能连网时没问题,不能连网时,就报以下类似错误: org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 75; schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans-4.1.x

Cause org xml sax SAXParseException lineNumber 32 column

1.错误描述 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' defined in URL [file:/F:/Tomcat/apache-tomcat-7.0.69/webapps/cmp/WEB-INF/classes/shiro-context.xml]: BeanPostProcessor before instantiation of

坑爹的Hibernate 映射文件错误提示org.xml.sax.SAXParseException

今天整整一个上午都在和hibernate做斗争,早上一来,继续昨天的项目开发,发现spring项目不能启动,从错误中看是hibernate错误,多半是hibernate配置有错误,关键是错误提示中显示“com.mdnet.travel.core.dao.ValidCodeDAOImpl”不能注入,经过仔细查看未见异常,而且这个文件好久没有改过了.没办法把hibernate的配置改成如下配置: <bean id="sessionFactory" class="org.sp

Cause: org.xml.sax.SAXParseException;

a.报错内容 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:331) [ERROR] - Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' d