xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence

http://blog.csdn.net/chenyanbo/article/details/6866941

xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence


说简单点当你解析别人的xml格式出现这个错误可能就是别人在生成xml时没有保存为utf-8的字符编码格式。

在中文版的window下java的默认的编码为GBK,也就是所虽然我们标识了要将xml保存为utf-8格式但实际上文件是以GBK格式来保存的,所以这也就是为什么能够我们使用GBK、GB2312编码来生成xml文件能正确的被解析,而以UTF-8格式生成的文件不能被xml解析器所解析的原因。


 

解决:

1、最简单就是把<?xml version="1.0" encoding="UTF-8"?>改成<?xml version="1.0" encoding="gbk"?>

2、或者把xml打开另存的时候把字符集改为UTF-8后保存

3、在代码解析的时候先把xml重新写一遍

4、直接dom4j读取的时候用io来读,修改字符编码

------------------------------------------------------------------------------------------------------------------------------

om4j处理中文之编码问题

转自:http://zhonghuaweixu.blog.163.com/blog/static/11793205920106693820542/


问题描述
 

在使用dom4j的时候发现有时会出现这样一个问题:无法以UTF-8编码格式成功保存xml文件,具体表现为保存后中文呈现乱码(如果没有乱码,说明保存前的编码没有设置成功,保存成了本地的gbk或者gb2312格式)再次读取的时候会报类似如下的错误:

Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.

Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence.

Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.

Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.

Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence.

2 字节 UTF-8 序列的无效字节 2。 Nested exception: 2 字节 UTF-8 序列的无效字


在dom4j的范例中新建一个xml文档的代码如下:

// 输出XML文档

try

{

    XMLWriter output = new XMLWriter(new FileWriter(new File("data/catalog.xml")));

    output.write(document);

    output.close();

}

catch (IOException e)

{

    System.out.println(e.getMessage());

}

错误原因分析

在上面的代码中输出使用的是FileWriter对象进行文件的输出。这就是不能正确进行文件编码的原因所在,Java中由Writer类继承下来的子类没有提供编码格式处理,所以dom4j也就无法对输出的文件进行正确的格式处理。这时候所保存的文件会以系统的默认编码对文件进行保存,在中文版的window下Java的默认的编码为GBK,也就是说虽然我们标识了要将xml保存为utf-8格式,但实际上文件是以GBK格式来保存的,所以这也就是为什么我们使用GBK、GB2312编码来生成xml文件能正确的被解析,而以UTF-8格式生成的文件不能被xml解析器所解析的原因。

如何解决问题?

首先我们看看dom4j是如何实现编码处理的,如下所示:
publicXMLWriter(OutputStream out) throws UnsupportedEncodingException {

    //System.out.println("In OutputStream");

    this.format = DEFAULT_FORMAT;

    this.writer = createWriter(out, format.getEncoding());

    this.autoFlush = true;

   namespaceStack.push(Namespace.NO_NAMESPACE);

}

publicXMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException {

    //System.out.println("In OutputStream,OutputFormat");

    this.format = format;

    this.writer = createWriter(out, format.getEncoding());

    this.autoFlush = true;

   namespaceStack.push(Namespace.NO_NAMESPACE);

}

/**

*GetanOutputStreamWriter,usepreferredencoding.

*/

protected Writer createWriter(OutputStream outStream, String

    encoding) throws UnsupportedEncodingException {

    returnnew BufferedWriter(

        new OutputStreamWriter( outStream, encoding )

    );

}

由上面的代码我们可以看出dom4j对编码并没有进行什么很复杂的处理,完全通过 Java本身的功能来完成。所以我们在使用dom4j生成xml文件时不应该直接在构建XMLWriter时,为其赋一个Writer对象,而应该通过一个OutputStream的子类对象来构建。也就是说在我们上面的代码中,不应该用FileWriter对象来构建xml文档,而应该使用FileOutputStream对象来构建,修改后的代码如下:

// 输出XML文档

try

{

    OutputFormat outFmt = new OutputFormat("\t", true);

    outFmt.setEncoding("UTF-8");

    XMLWriter output = new XMLWriter(new FileOutputStream(filename), outFmt);  

    output.write(document);

    output.close();

}

catch (IOException e)

{

    System.out.println(e.getMessage());

}

如何读取呢?

public List extractXMLText(File inputXml, String node)

{

    List texts = null;

    try

    {

       // 使用SAXReader解析XML文档,SAXReader包含在org.dom4j.io包中。

       // inputXml是由xml文件创建的java.io.File。

       SAXReader saxReader = new SAXReader();

       saxReader.setEncoding("UTF-8");

       Document document = saxReader.read(inputXml);

          

       texts = document.selectNodes(node); // 获取sentence列表

    }

catch (DocumentException e)

  {

       System.out.println(e.getMessage());

  }

  return texts;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值