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")));
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的更多相关文章
- 关于xml加载提示: Error on line 1 of document : 前言中不允许有内容
我是在java中做的相关测试, 首先粘贴下报错: 读取xml配置文件:xmls\property.xml org.dom4j.DocumentException: Error on line 1 of ...
- python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib
python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...
- RSS阅读器(一)——dom4j读取xml(opml)文件
接触java不久,偶有收获,最近想做一个web版RSS阅读器来锻炼一下.手头有几个从不同版本的foxmail中导出的opml文件,大家应该都知道,opml文件就是xml格式的.那么就先从这里入手,练习 ...
- 使用dom4j 读取XML文件
第一次接触dom4j的时候,感觉这个东西很神秘,因为之前虽然知道XML文件吧,但从来没有用过,一直感觉XML肯定不好操作.当得知,dom4j可以很容易的操作读取XML文件时,不免有些好奇,那么,用do ...
- DOM4J读取XML文件
最近在做DRP的项目,其中涉及到了读取配置文件,用到了DOM4J,由于是刚开始接触这种读取xml文件的技术,好奇心是难免的,于是在网上又找了一些资料,这里就结合找到的资料来谈一下读取xml文件的4中方 ...
- 【XML】利用Dom4j读取XML文档以及写入XML文档
Dom4j简介 dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,它的性能 ...
- python 读取文件时报错UnicodeDecodeError
python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...
- 使用dom4j读取xml连接数据库与之单例模式
使用dom4j读取xml ,加入jar包 dom4j-1.6.1.jar jaxen-1.1-beta-6.jar public class XmlConfigReader { //懒汉式,延迟加载 ...
- mysql启动时报错:Starting MySQL... ERROR! The server quit without updating PID file (/opt/mysql/data/mysql.pid)
mysql启动报错Starting MySQL... ERROR! The server quit without updating PID file (/opt/mysql/data/mysql.p ...
随机推荐
- R语言 ggplot2 画平滑图
library(splines) library(ggplot2) dt1 <- structure(list(Age = structure(c(1L, 1L, 1L, 1L, 1L, 1L, ...
- 《FPGA全程进阶---实战演练》第十二章 二进制码与格雷码PK
大家在写程序的时候,可能会听闻,什么独热码,什么格雷码,什么二进制码等等,本节意在解释这几种编码之间的区别和优势以及用verilog怎么去实现,下面先介绍这几种编码的区别. 1 基础理论部分 1.1 ...
- python进行数据分析---python3卡方
数学公式思路: 代码: 使用典型的pandas进行的逻辑操作 同时也指针对了pandas https://github.com/renfanzi/python3_Variance_Chisquare. ...
- 【CentOS】CentOS7.0 mysql与卸载
mysql安装: 在使用命令 yum list mysql-server 安装mysql的时候,发现没有mysql的包.这时候,我们需要下载一个 下载包 wget http://repo.mysql. ...
- python 读写配置文件
使用python读写配置文件,写个demo测试一下. #!/usr/bin/env python import os import ConfigParser # 如果文件不存在,就穿件文件. if o ...
- linux中查看某个端口(port)
查看 27017 端口: netstat -anp | grep 27017 Proto Recv-Q Send-Q Local Address ...
- Java如何以短格式显示月份?
在Java中,如何显示短格式的月份名称? 使用DateFormatSymbols().DateFormatSymbols类的getShortMonths()方法,本示例显示了几个月的简写名称. pac ...
- e836. 设置JTabbedPane中卡片的提示语
There are two ways to set a tool tip on a tab. The first is to specify it when the tab is created; t ...
- python2除法保留小数部分
转载:http://www.cnblogs.com/yhleng/p/9223944.html 1.python2和python3除法的最大区别: python2: print 500/1000 py ...
- nodejs npm 使用淘宝 NPM 镜像
使用淘宝 NPM 镜像 大家都知道国内直接使用 npm 的官方镜像是非常慢的,这里推荐使用淘宝 NPM 镜像. 淘宝 NPM 镜像是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读), ...