你的位置:首页 > 操作系统

[操作系统]pull解析器: 反序列化与序列化


 

  pull解析器:反序列化  读取

 

 1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.util.ArrayList; 4 import java.util.List; 5  6 import org. 7 import org. 8 import org. 9 10 public class Read11 12   /**13    *  pull解析器:反序列化   读取14    * @param args15    * @throws 16    * @throws IOException 17   */18   public static void main(String[] args) throws 19 20     //1.导包21     //2.获取解析器工厂22      23     //3.根据工厂获取解析器对象24      factory.newPullParser();25     //4.设置输入流26     parser.setInput(new FileInputStream("src/student.);27     //5.解析28     List<Student> list = null;29     Student stu = null;30     //获取对应时间的类型31     int type = parser.getEventType(); 32     while(type != 33       //获取标签名称34       String tagname = parser.getName();35       switch(type){36       case 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 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解析器:序列化    将一个对象的数据  写入到

 

 1 import java.io.FileOutputStream; 2 import java.io.IOException; 3  4 import org. 5 import org. 6 import org. 7  8 public class Write 9 10   /**pull解析器:序列化  将一个对象的数据 写入到11    * @param args12    * @throws 13    * @throws IOException 14   */15   public static void main(String[] args) throws 16 17     Student stu = new Student("s_001", "小飞飞", 23);18     //1.导包19     //2.获取解析器工厂20      21     //3.根据工厂获取解析器对象22      factory.newSerializer();23     //4.设置输出流24     serializer.setOutput(new FileOutputStream("src/sss.);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 id18   */19   public String getId() {20     return id;21   }22   /**23    * @param id the id to set24   */25   public void setId(String id) {26     this.id = id;27   }28   /**29    * @return the name30   */31   public String getName() {32     return name;33   }34   /**35    * @param name the name to set36   */37   public void setName(String name) {38     this.name = name;39   }40   /**41    * @return the age42   */43   public int getAge() {44     return age;45   }46   /**47    * @param age the age to set48   */49   public void setAge(int age) {50     this.age = age;51   }52   /* (non-Javadoc)53    * @see java.lang.Object#toString()54   */55   @Override56   public String toString() {57     return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";58   }59 60 }

 

  student.

 1 <??> 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包):

  k