zoukankan      html  css  js  c++  java
  • Java使用dom4j读取xml时报错:org.dom4j.DocumentException: Error on line 2 of document : Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence

    1.Java使用dom4j读取xml时报错:

      org.dom4j.DocumentException: Error on line 2 of document  : Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence

    2.错误原因:

      接口返回的诗句编码是GBK,而我们代码中虽然指定了InputSource的编码,但是没有对解析xml时进行编码。

    3.未修改的代码:

      SAXReader reader = new SAXReader(false);
      InputSource source = new InputSource(new ByteArrayInputStream(xml.getBytes()));
      source.setEncoding("UTF-8");
      Document doc = reader.read(source);

     4.改后的代码:

        SAXReader reader = new SAXReader(false);
        InputSource source = new InputSource(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        source.setEncoding("UTF-8");
        Document doc = reader.read(source);

      增加了对xml字符串转字节的时候转码操作。

    或者使用:

        SAXReader reader = new SAXReader(false);
        Document doc = saxReader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
  • 相关阅读:
    这几天都是在公司慢待
    电脑没有关机可能出现发博文dns异常(write)
    DOS/VBS
    SourceInsight
    CevaEclipse
    C/C++
    Matlab
    Matlab
    C语言
    Matlab
  • 原文地址:https://www.cnblogs.com/xbq8080/p/7346909.html
Copyright © 2011-2022 走看看