pull解析器: 反序列化与序列化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pull解析器: 反序列化与序列化相关的知识,希望对你有一定的参考价值。

 

  pull解析器:反序列化  读取xml文件来获取一个对象的数据

 

 1 import java.io.FileInputStream;
 2 import java.io.IOException;
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import org.xmlpull.v1.XmlPullParser;
 7 import org.xmlpull.v1.XmlPullParserException;
 8 import org.xmlpull.v1.XmlPullParserFactory;
 9 
10 public class ReadXmlTest {
11 
12     /**
13      *   pull解析器:反序列化     读取xml文件来获取一个对象的数据
14      * @param args
15      * @throws XmlPullParserException 
16      * @throws IOException 
17      */
18     public static void main(String[] args) throws XmlPullParserException, IOException {
19 
20         //1.导包
21         //2.获取解析器工厂
22         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
23         //3.根据工厂获取解析器对象
24         XmlPullParser parser = factory.newPullParser();
25         //4.设置输入流
26         parser.setInput(new FileInputStream("src/student.xml"),"utf-8");
27         //5.解析
28         List<Student> list = null;
29         Student stu = null;
30         //获取对应时间的类型
31         int type = parser.getEventType(); 
32         while(type != XmlPullParser.END_DOCUMENT){
33             //获取标签名称
34             String tagname = parser.getName();
35             switch(type){
36             case XmlPullParser.START_TAG:
37                 //判断标签名称
38                 if("students".equals(tagname)){
39                     //创建集合
40                     list = new ArrayList<Student>();
41                 }else if("student".equals(tagname)){
42                     //创建对象
43                     stu = new Student();
44                     //获取ID属性值
45                     String id = parser.getAttributeValue(0);
46                     //赋值
47                     stu.setId(id);
48                 }else if("name".equals(tagname)){
49                     //获取标签体的文本
50                     String name = parser.nextText();
51                     //赋值
52                     stu.setName(name);
53                 }else if("age".equals(tagname)){
54                     //获取标签体的文本
55                     String age = parser.nextText();
56                     //赋值
57                     stu.setAge(Integer.parseInt(age));
58             }
59                 break;
60             case XmlPullParser.END_TAG:
61                 if("student".equals(tagname)){
62                     //将对象添加到集合中
63                     list.add(stu);
64                     stu = null;
65                 }
66                 break;
67             default:
68                 break;
69             }
70         //向下走一步
71         parser.next();
72         //重新赋值
73         type = parser.getEventType();
74         }
75         //6.输出
76         System.out.println(list);
77     }
78 }

 

------------------------------------------------------------------------------------------------

 

  pull解析器:序列化    将一个对象的数据  写入到xml文件中

 

 1 import java.io.FileOutputStream;
 2 import java.io.IOException;
 3 
 4 import org.xmlpull.v1.XmlPullParserException;
 5 import org.xmlpull.v1.XmlPullParserFactory;
 6 import org.xmlpull.v1.XmlSerializer;
 7 
 8 public class WriteXmlTest {
 9 
10     /**pull解析器:序列化   将一个对象的数据  写入到xml文件中
11      * @param args
12      * @throws XmlPullParserException 
13      * @throws IOException 
14      */
15     public static void main(String[] args) throws XmlPullParserException, IOException {
16 
17         Student stu = new Student("s_001", "小飞飞", 23);
18         //1.导包
19         //2.获取解析器工厂
20         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
21         //3.根据工厂获取解析器对象
22         XmlSerializer serializer = factory.newSerializer();
23         //4.设置输出流
24         serializer.setOutput(new FileOutputStream("src/sss.xml"),"utf-8");
25         //5.写入
26         //5.1写文档声明  参数1:encoding属性值,参数2:standalone属性值
27         serializer.startDocument("utf-8", true);
28         //5.2写入开始跟标签
29         serializer.startTag(null, "students");
30         for(int i=0; i <5; i++){
31             //5.3写入student标签
32             serializer.startTag(null, "student");
33             
34             serializer.attribute(null, "id", stu.getId());
35             
36             serializer.startTag(null, "name");
37             serializer.text(stu.getName());
38             serializer.endTag(null, "name");
39             
40             serializer.startTag(null, "age");
41             serializer.text(String.valueOf(stu.getAge()));
42             serializer.endTag(null, "age");
43             
44             serializer.endTag(null, "student");
45         }
46         //5.4写入结束标签
47         serializer.endTag(null, "students");
48         //5.5写入结束文档
49         serializer.endDocument();
50         //反应结果
51         System.out.println("写入完成,请去查看!");
52     }
53 }

 

  Student类

 

 1 public class Student {
 2 
 3     private String id;
 4     private String name;
 5     private int age;
 6     
 7     public Student() {
 8         super();
 9     }
10     public Student(String id, String name, int age) {
11         super();
12         this.id = id;
13         this.name = name;
14         this.age = age;
15     }
16     /**
17      * @return the id
18      */
19     public String getId() {
20         return id;
21     }
22     /**
23      * @param id the id to set
24      */
25     public void setId(String id) {
26         this.id = id;
27     }
28     /**
29      * @return the name
30      */
31     public String getName() {
32         return name;
33     }
34     /**
35      * @param name the name to set
36      */
37     public void setName(String name) {
38         this.name = name;
39     }
40     /**
41      * @return the age
42      */
43     public int getAge() {
44         return age;
45     }
46     /**
47      * @param age the age to set
48      */
49     public void setAge(int age) {
50         this.age = age;
51     }
52     /* (non-Javadoc)
53      * @see java.lang.Object#toString()
54      */
55     @Override
56     public String toString() {
57         return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
58     }
59 
60 }

 

  student.xml

 1 <?xml version=‘1.0‘ encoding=‘utf-8‘ ?>
 2 
 3 <students>
 4     <student id=‘s001‘>
 5         <name>zhangsan</name>
 6         <age>23</age>
 7     </student>
 8         
 9     <student id="s002">
10         <name>lisi</name>
11         <age>24</age>
12     </student>
13 
14     <student id="s003">
15         <name>王五</name>
16         <age>25</age>
17     </student>
18     
19 </students>

 

导入的包名称(pull解析器jar包):

  kxml2-2.3.0.jar

  xmlpull_1_1_3_4c.jar

 

以上是关于pull解析器: 反序列化与序列化的主要内容,如果未能解决你的问题,请参考以下文章

RestFramework

rest_framework

Day4 闭包装饰器decorator迭代器与生成器面向过程编程三元表达式列表解析与生成器表达式序列化与反序列化

Django rest_framewok框架的基本组件

Day01—xml的序列化xml的pull解析

Flink实战系列Flink 1.14.0 消费 kafka 数据自定义反序列化器