java xmldecoder反序列化
1、代码
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
public class xmldecoder {
public static void XML_Decode_Deserialize(String path) throws Exception{
File file = new File(path); //创建一个文件函数
FileInputStream fis = new FileInputStream(file); //读入文件
BufferedInputStream bis = new BufferedInputStream(fis); //读入缓存
XMLDecoder xd = new XMLDecoder(bis); //xml解码
xd.readObject(); //反序列化
xd.close();
}
public static void main(String[] args) throws Exception {
String path = "E:\\java\\javale\\xmldecoder\\src\\main\\java\\poc.xml";
XML_Decode_Deserialize(path); //没什么好说的调用xmldecodedeserialize函数对xml文件进行解码并且反序列化
}
}
2、调试分析
在xd.readObject处打上断点开始分析
进入readObject()方法内,发现会调用parsingComplete()方法,parsingComplete()方法的主要作用是用来判断是否解析完成
readObject()方法
public Object readObject() {
return (parsingComplete()) //返回parsingComplete的值为真则调用this.array[this.index++],为假则调用null
? this.array[this.index++]
: null;
}
parsingComplete()方法
在parsingComplete()方法内会调用XMLDecoder.this.handler.parse()方法来进行解析,其中this.handler指向的是DocumentHandler,也就是说实际上是调用的DocumentHandler的parse()方法来解析xml文档
private boolean parsingComplete() {
if (this.input == null) {
return false;
}
if (this.array == null) {
if ((this.acc == null) && (null != System.getSecu