java xml utf-8 乱码_出现下面的的XML解析异常的根本原因是什么,表层原因应该是UTF-8解析过程中遇到中文乱码?...

本文主要是介绍java xml utf-8 乱码_出现下面的的XML解析异常的根本原因是什么,表层原因应该是UTF-8解析过程中遇到中文乱码?...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

事实上XML解析器尝试使用UTF-8编码来进行解析,但是解析到中文的时候,发现编码不符合UTF-8编码的规则了,然后就报错了,这也是为什么我删除中文之后就正确了的原因。

到解析xml时发现xml定义的编码为utf8或者没有定义编码的时候,都会采用utf8解码。

具体就是用到下面那个类,用于读取输入流。

我们知道,utf8编码每个字符,所用的字节数是不一样的,也就是不定长编码。

这种编码的方式。最主要的还是用于网络传输。在网上中可以很明确的知道,哪几个字节,构成一个字符,无论中间被截断还是缺失,都可以分辨出来。下面的代码就是在读取,字节的过程中,根据,编码的特点,校验,所读取的字节,是否符合,编码规则。

当不符合编码规则的时候,就会抛出异常。

具体规则介绍如下,其实就是utf8的编码规则。

每个字节的前n位为1,说明包含本字节在内的n个字节为一个字符。

第n+1位为0

更为详细的请参看

Unicode 和 UTF-8 有何区别?​www.zhihu.comzhihu-card-default.svg

21 package com.sun.org.apache.xerces.internal.impl.io;

22

23 import java.io.InputStream;

24 import java.io.IOException;

25 import java.io.Reader;

26

27 import java.util.Locale;

28 import com.sun.org.apache.xerces.internal.util.MessageFormatter;

29 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;

30

31 import com.sun.xml.internal.stream.util.BufferAllocator;

32 import com.sun.xml.internal.stream.util.ThreadLocalBufferAllocator;

33

34 /**

35 *

A UTF-8 reader.

36 *

37 * @xerces.internal

38 *

39 * @author Andy Clark, IBM

40 *

41 */

42 public class UTF8Reader

43 extends Reader {

44

45 //

46 // Constants

47 //

48

49 /** Default byte buffer size (2048). */

50 public static final int DEFAULT_BUFFER_SIZE = 2048;

51

52 // debugging

53

54 /** Debug read. */

55 private static final boolean DEBUG_READ = false;

56

57 //

58 // Data

59 //

60

61 /** Input stream. */

62 protected InputStream fInputStream;

63

64 /** Byte buffer. */

65 protected byte[] fBuffer;

66

67 /** Offset into buffer. */

68 protected int fOffset;

69

70 /** Surrogate character. */

71 private int fSurrogate = -1;

72

73 // message formatter; used to produce localized

74 // exception messages

75 private MessageFormatter fFormatter = null;

76

77 //Locale to use for messages

78 private Locale fLocale = null;

79

80 //

81 // Constructors

82 //

83

84 /**

85 * Constructs a UTF-8 reader from the specified input stream

86 * using the default buffer size. Primarily for testing.

87 *

88 * @param inputStream The input stream.

89 */

90 public UTF8Reader(InputStream inputStream) {

91 this(inputStream, DEFAULT_BUFFER_SIZE, new XMLMessageFormatter(), Locale.getDefault());

92 } // (InputStream, MessageFormatter)

93

94 /**

95 * Constructs a UTF-8 reader from the specified input stream

96 * using the default buffer size and the given MessageFormatter.

97 *

98 * @param inputStream The input stream.

99 * @param messageFormatter given MessageFormatter

100 * @param locale Locale to use for messages

101 */

102 public UTF8Reader(InputStream inputStream, MessageFormatter messageFormatter,

103 Locale locale) {

104 this(inputStream, DEFAULT_BUFFER_SIZE, messageFormatter, locale);

105 } // (InputStream, MessageFormatter, Locale)

106

107 /**

108 * Constructs a UTF-8 reader from the specified input stream,

109 * buffer size and MessageFormatter.

110 *

111 * @param inputStream The input stream.

112 * @param size The initial buffer size.

113 * @param messageFormatter the formatter for localizing/formatting errors.

114 * @param locale the Locale to use for messages

115 */

116 public UTF8Reader(InputStream inputStream, int size,

117 MessageFormatter messageFormatter, Locale locale) {

118 fInputStream = inputStream;

119 BufferAllocator ba = ThreadLocalBufferAllocator.getBufferAllocator();

120 fBuffer = ba.getByteBuffer(size);

121 if (fBuffer == null) {

122 fBuffer = new byte[size];

123 }

124 fFormatter = messageFormatter;

125 fLocale = locale;

126 } // (InputStream, int, MessageFormatter, Locale)

127

128 //

129 // Reader methods

130 //

131

132 /**

133 * Read a single character. This method will block until a character is

134 * available, an I/O error occurs, or the end of the stream is reached.

135 *

136 *

Subclasses that intend to support efficient single-character input

137 * should override this method.

138 *

139 * @return The character read, as an integer in the range 0 to 16383

140 * (0x00-0xffff), or -1 if the end of the stream has

141 * been reached

142 *

143 * @exception IOException If an I/O error occurs

144 */

145 public int read() throws IOException {

146

147 // decode character

148 int c = fSurrogate;

149 if (fSurrogate == -1) {

150 // NOTE: We use the index into the buffer if there are remaining

151 // bytes from the last block read. -Ac

152 int index = 0;

153

154 // get first byte

155 int b0 = index == fOffset

156 ? fInputStream.read() : fBuffer[index++] & 0x00FF;

157 if (b0 == -1) {

158 return -1;

159 }

160

161 // UTF-8: [0xxx xxxx]

162 // Unicode: [0000 0000] [0xxx xxxx]

163 if (b0 < 0x80) {

164 c = (char)b0;

165 }

166

167 // UTF-8: [110y yyyy] [10xx xxxx]

168 // Unicode: [0000 0yyy] [yyxx xxxx]

169 else if ((b0 & 0xE0) == 0xC0 && (b0 & 0x1E) != 0) {

170 int b1 = index == fOffset

171 ? fInputStream.read() : fBuffer[index++] & 0x00FF;

172 if (b1 == -1) {

173 expectedByte(2, 2);

174 }

175 if ((b1 & 0xC0) != 0x80) {

176 invalidByte(2, 2, b1);

177 }

178 c = ((b0 << 6) & 0x07C0) | (b1 & 0x003F);

179 }

180

181 // UTF-8: [1110 zzzz] [10yy yyyy] [10xx xxxx]

182 // Unicode: [zzzz yyyy] [yyxx xxxx]

183 else if ((b0 & 0xF0) == 0xE0) {

184 int b1 = index == fOffset

185 ? fInputStream.read() : fBuffer[index++] & 0x00FF;

186 if (b1 == -1) {

187 expectedByte(2, 3);

188 }

189 if ((b1 & 0xC0) != 0x80

190 || (b0 == 0xED && b1 >= 0xA0)

191 || ((b0 & 0x0F) == 0 && (b1 & 0x20) == 0)) {

192 invalidByte(2, 3, b1);

193 }

194 int b2 = index == fOffset

195 ? fInputStream.read() : fBuffer[index++] & 0x00FF;

196 if (b2 == -1) {

197 expectedByte(3, 3);

198 }

199 if ((b2 & 0xC0) != 0x80) {

200 invalidByte(3, 3, b2);

201 }

202 c = ((b0 << 12) & 0xF000) | ((b1 << 6) & 0x0FC0) |

203 (b2 & 0x003F);

204 }

205

206 // UTF-8: [1111 0uuu] [10uu zzzz] [10yy yyyy] [10xx xxxx]*

207 // Unicode: [1101 10ww] [wwzz zzyy] (high surrogate)

208 // [1101 11yy] [yyxx xxxx] (low surrogate)

209 // * uuuuu = wwww + 1

210 else if ((b0 & 0xF8) == 0xF0) {

211 int b1 = index == fOffset

212 ? fInputStream.read() : fBuffer[index++] & 0x00FF;

213 if (b1 == -1) {

214 expectedByte(2, 4);

215 }

216 if ((b1 & 0xC0) != 0x80

217 || ((b1 & 0x30) == 0 && (b0 & 0x07) == 0)) {

218 invalidByte(2, 3, b1);

219 }

220 int b2 = index == fOffset

221 ? fInputStream.read() : fBuffer[index++] & 0x00FF;

222 if (b2 == -1) {

223 expectedByte(3, 4);

224 }

225 if ((b2 & 0xC0) != 0x80) {

226 invalidByte(3, 3, b2);

227 }

228 int b3 = index == fOffset

229 ? fInputStream.read() : fBuffer[index++] & 0x00FF;

230 if (b3 == -1) {

231 expectedByte(4, 4);

232 }

233 if ((b3 & 0xC0) != 0x80) {

234 invalidByte(4, 4, b3);

235 }

236 int uuuuu = ((b0 << 2) & 0x001C) | ((b1 >> 4) & 0x0003);

237 if (uuuuu > 0x10) {

238 invalidSurrogate(uuuuu);

239 }

240 int wwww = uuuuu - 1;

241 int hs = 0xD800 |

242 ((wwww << 6) & 0x03C0) | ((b1 << 2) & 0x003C) |

243 ((b2 >> 4) & 0x0003);

244 int ls = 0xDC00 | ((b2 << 6) & 0x03C0) | (b3 & 0x003F);

245 c = hs;

246 fSurrogate = ls;

247 }

248

249 // error

250 else {

251 invalidByte(1, 1, b0);

252 }

253 }

254

255 // use surrogate

256 else {

257 fSurrogate = -1;

258 }

259

260 // return character

261 if (DEBUG_READ) {

262 System.out.println("read(): 0x"+Integer.toHexString(c));

263 }

264 return c;

265

266 } // read():int

267

这篇关于java xml utf-8 乱码_出现下面的的XML解析异常的根本原因是什么,表层原因应该是UTF-8解析过程中遇到中文乱码?...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/280705

相关文章

将Mybatis升级为Mybatis-Plus的详细过程

《将Mybatis升级为Mybatis-Plus的详细过程》本文详细介绍了在若依管理系统(v3.8.8)中将MyBatis升级为MyBatis-Plus的过程,旨在提升开发效率,通过本文,开发者可实现... 目录说明流程增加依赖修改配置文件注释掉MyBATisConfig里面的Bean代码生成使用IDEA生

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain