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

背景:

解析本地的一个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?");
    }
  }








  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值