SAXParseException: The encoding "GBK" is not supported --- 解决办法与分析

原创 2015年07月08日 22:57:34

背景:

解析本地的一个XML文件,文件编码GBK,XML文件头:<?xml version="1.0" encoding="GBK" ?> 

可能的代码:

    1.读取XML文件,获得document对象            
                   SAXReader reader = new SAXReader();               
                   Document   document = reader.read(new File("csdn.xml"));
    2.解析XML形式的文本,得到document对象.
                   String text = "<csdn></csdn>";             
                   Document document = DocumentHelper.parseText(text);
    3.主动创建document对象.
                  Document document = DocumentHelper.createDocument();              //创建根节点
                  Element root = document.addElement("csdn");


运行时抛出异常

SAXParseException: The encoding "GBK" is not supported。

按照网络上说的方法:检查xml的格式,utf-8  去BOM 诸如此类都没有办法解决。


后面发现同样的代码可以在另外正常运行,开始比较差异。

原来是jar包惹的祸:

dom4j-1.6.1.jar 依赖于xerces  对xml解析的实现。

而一般使用的xerces-2.6.2.jar 并不支持GBK的编码格式。

为什么有些情况下又没问题呢?


关键jdk的版本,jdk6后的rt包实现了xml解析,支持gbk,故2种解决方案。

1. 升级xerces.jar

2. 升级jdk 或者(关掉1.4兼容API)


=====原因再分析==========

http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/SAXReader.html

The actual SAX parser that is used by this class is configurable so you can use your favourite SAX parser if you wish. DOM4J comes configured with its own SAX parser so you do not need to worry about configuring the SAX parser.

To explicitly configure the SAX parser that is used via Java code you can use a constructor or use the setXMLReader(XMLReader)or setXMLReaderClassName(String) methods.

If the parser is not specified explicitly then the standard SAX policy of using the org.xml.sax.driver system property is used to determine the implementation class of XMLReader.

If the org.xml.sax.driver system property is not defined then JAXP is used via reflection (so that DOM4J is not explicitly dependent on the JAXP classes) to load the JAXP configured SAXParser. If there is any error creating a JAXP SAXParser an informational message is output and then the default (Aelfred) SAX parser is used instead.

-------------call trace---------

org.dom4j.io.SAXReader.read(XXXX)

org.dom4j.io.SAXReader.read(InputSource in)

org.dom4j.io.SAXReader.getXMLReader(SAXReader.java:715)

org.dom4j.io.SAXReader.createXMLReader(SAXReader.java:894)

org.dom4j.io.SAXHelper.createXMLReader(SAXHelper.java:83)

org.xml.sax.helpers.XMLReaderFactory.createXMLReader()


先查找 property的值

private static final String property = "org.xml.sax.driver";
public static XMLReader createXMLReader ()
	throws SAXException
    {
	String		className = null;
	ClassLoader	loader = NewInstance.getClassLoader ();
	
	// 1. try the JVM-instance-wide system property
	try { className = System.getProperty (property); }
	catch (RuntimeException e) { /* normally fails for applets */ }

	// 2. if that fails, try META-INF/services/
	if (className == null) {
	    try {
		String		service = "META-INF/services/" + property;
		InputStream	in;
		BufferedReader	reader;

		if (loader == null)
		    in = ClassLoader.getSystemResourceAsStream (service);
		else
		    in = loader.getResourceAsStream (service);

		if (in != null) {
		    reader = new BufferedReader (
			    new InputStreamReader (in, "UTF8"));
		    className = reader.readLine ();
		    in.close ();
		}
	    } catch (Exception e) {
	    }
	}
      // 3. Distro-specific fallback
       if (className == null) {
      // BEGIN DISTRIBUTION-SPECIFIC

        // EXAMPLE:
        // className = "com.example.sax.XmlReader";
        // or a $JAVA_HOME/jre/lib/*properties setting...
            className = "com.sun.org.apache.xerces.internal.parsers.SAXParser";

      // END DISTRIBUTION-SPECIFIC
     }
   // do we know the XMLReader implementation class yet?
    if (className != null)
        return loadClass (loader, className);

    // 4. panic -- adapt any SAX1 parser
    try {
        return new ParserAdapter (ParserFactory.makeParser ());
    } catch (Exception e) {
        throw new SAXException ("Can't create default XMLReader; "
            + "is system property org.xml.sax.driver set?");
    }
  }








版权声明:本文为博主原创文章,转载请声明链接。 举报

相关文章推荐

SAXParseException: The encoding "GBK" is not supported --- 解决办法与分析

The encoding "GBK" is not supported --- 解决办法与分析     1.读取XML文件,获得document对象       &#...

解决:DB2com.ibm.db2.jcc.b.DisconnectException: encoding not supported!!

环境是:jdk:1.6    DB2:V9.7 tomcat5 错误描述:使用JDBC连接一下(Driver:com.ibm.db2.jcc.DB2Driver),发生如下错误: 原因分析: ...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine 解决办法

64位操作系统中使用OLEDB去访问access数据库,出现 The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local...

linux下”is not in the sudoers file“问题的解决办法

原因当有时候我们使用sudo命令切换用户的时候可能会遇到提示以下错误:xxx is not in the sudoers file. This incident will be reportedxxx...

关于 Android中googleMaps的Geocoder, “the service is not available”解决办法

Android中用Json解析googleMaps经纬度          大家在用googleMaps中的Geocoder进行地理位置反转经纬度时, 会出现这个错误Geocoder, “the...

eclipse中出现The con“structor 'NimbusLookAndFeel()' is not API ”的解决办法

在eclipse中,有时会出现如下的错误: 它提示的错误信息是这样子的: Access restriction: The constructor ‘NimbusLookAndFeel()’...

Type interface is not known to the MapperRegistry.的解决办法

映射器(mappers)既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要定义 SQL 映射语句了。但是首先我们需要告诉 MyBatis 到哪里去找到这些语句。 Java 在自动查找这方...

安装python出现is not a supported wheel on this platform解决办法

安装python库numpy的时候出现numpy-1.12.1+mkl-cp27-cp27m-win32.whl is not a supported wheel on this platform ...

根据wsdl生成客户端:Use of SOAP Encoding is not supported

一.生成方式选择接收到服务端第三方给的wsdl文件,需要在本地生成客户端调用。调用或生成客户端的方式有很多种,可能你会使用eclipse上的插件、IDEA上的插件、使用xfire简单的调用方式等,会碰...

ADVM/ACFS is not supported on centos-release-5-5.el5.centos 解决方法

在 http://www.rampant-books.com/t_hunter_using_oracle_acfs_on_linux_part1.htm 看到有此解决办法, 使用后,此问题是解决了。...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)