当前位置:网站首页>Dom4J的Bug
Dom4J的Bug
2022-07-28 19:04:00 【android_cai_niao】
依赖:
implementation 'org.dom4j:dom4j:2.1.3'
代码:
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.ByteArrayInputStream;
public class JavaMain {
public static void main(String[] args) {
String xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?><Query>123</Query>";
try {
SAXReader reader = new SAXReader();
reader.setEncoding("gb2312");
Document document = reader.read(new ByteArrayInputStream(xml.getBytes("gb2312")));
Element rootElement = document.getRootElement();
String text = rootElement.getText();
System.out.println("text = " + text);
} catch (Exception e) {
e( "xml解析异常,xml = " + xml, e);
}
}
public static void e(String msg) {
System.out.println(msg);
}
public static void e(String msg, Throwable e) {
e(msg);
e.printStackTrace();
}
}
这个代码在IntelliJ中运行是OK的,但是在AndroidStudio中运行却报了异常,百思不得其解,后来看了一下以前学习Dom4J时的笔记才发现,SAXReader 不需要设置编码,因为SAXReader 在解析xml的时候就能知道用什么编码来解析了(xml文件中包含有编码信息),所以不用设置,于是把这个代码删除:reader.setEncoding("gb2312");,在IntelliJ和AndroidStudio中均可正常运行!那setEncoding()的功能究竟是什么?文档声明如下:
/** * Sets encoding used for InputSource (null means system default encoding) * * @param encoding - is encoding used for InputSource */
public void setEncoding(String encoding) {
this.encoding = encoding;
}
字面理解就是用于设置输入源的编码。这么理解的话那我们前面的设置也没问题啊,是Dom4J有Bug?
边栏推荐
- Explain various coordinate systems in unity in detail
- unity-shader-1
- How to use redis to realize things and locks?
- 不懂就问,快速成为容器服务进阶玩家!
- Oracle library access is slow. Why?
- Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~
- The average altitude is 4000 meters! We built a cloud on the roof of the world
- NAT实验演示(Huawei交换机设备配置)
- C # basic 7-iterator and coroutine
- 微信小程序的分包加载
猜你喜欢

Explain the camera in unity and its application
Looking at SQL optimization from the whole process of one query

JS drag and drop alert pop-up plug-in

Space shooting Lesson 10: score (painting and writing)

Integrating database Ecology: using eventbridge to build CDC applications

Introduction to redis II: RedHat 6.5 installation and use

Meaning of disk status of EMC DataDomain

JS fly into JS special effect pop-up login box

《软件设计师考试》易混淆知识点

Laser slam:logo-loam --- code compilation, installation and gazebo test
随机推荐
PostgreSQL数据库删库前是不是需要把所有连接断开才能删除?
3D laser slam: Interpretation of logo-loam paper - Introduction
It is not only convenient, safe + intelligent, but also beautiful. Fluorite releases the Big Dipper face lock dl30f and Aurora face video lock y3000fv
Three deletion strategies and eviction algorithm of redis
Explain various coordinate systems in unity in detail
Oracle库访问很慢,是什么原因?
怎样搭建企业内部维基百科
Cartoon JS shooting game source code
使用ORDER BY 排序
Unity foundation 1 - event execution sequence, custom events
Easynlp Chinese text and image generation model takes you to become an artist in seconds
C # basic 7-iterator and coroutine
Redis 3.0 source code analysis - data structure and object SDS list Dict
The EMC vnx5200 fault light is on, but there is no hardware fault prompt
微信小程序的分包加载
Redis 3.0源码分析-数据结构与对象 SDS LIST DICT
JS drag and drop alert pop-up plug-in
Unity typewriter teaches you three ways
【1331. 数组序号转换】
Space shooting lesson 14: player life