zoukankan      html  css  js  c++  java
  • 序列化和反序列化的示例代码

    : 需要借助第三方kxml2-2.3.0.jarxmlpull_1_1_3_4c.jar文件

    public class Student {
       private String sid;
       private String name;
       private int age;
       private double score;
       public String getSid() {
          return sid;
       }
       public void setSid(String sid) {
          this.sid = sid;
       }
       public String getName() {
          return name;
       }
       public void setName(String name) {
          this.name = name;
       } 
       public int getAge() {
          return age;
       }
       public void setAge(int age) {
         this.age = age;
       }
       public double getScore() {
          return score;
       }
       public void setScore(double score) {
          this.score = score;
       } 
       @Override
       public String toString() {
          return "Student [sid=" + sid + ", name=" + name + ", age=" + age + ", score=" + score + "]";
       }
    }
    
    import java.io.FileOutputStream;
    import org.xmlpull.v1.XmlPullParserFactory;
    import org.xmlpull.v1.XmlSerializer;
    import com.loaderman.domain.Student;
    
    /**
     * 写入数据
     * @author super
     *
     */
    
    public class PullDemo4 {
       public static void main(String[] args) throws Exception {
          Student stu = new Student();
          stu.setSid("s003");
          stu.setName("赵六");
          stu.setAge(66);
          stu.setScore(66.6);
    
          //1.创建解析器工厂
          XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
          //2.获取解析器
          XmlSerializer serializer = factory.newSerializer();
           //3.设置输出流
          serializer.setOutput(new FileOutputStream("s.xml"), "utf-8");
          //4.写入文档声明
          serializer.startDocument("utf-8", true);
          serializer.startTag(null, "students");
          serializer.startTag(null, "student");
          serializer.attribute(null, "sid", stu.getSid());
          serializer.startTag(null, "name");
          serializer.text(stu.getName());
          serializer.endTag(null, "name");     
          serializer.startTag(null, "age");
          serializer.text(String.valueOf(stu.getAge()));
          serializer.endTag(null, "age");
          serializer.startTag(null, "score");
          serializer.text(String.valueOf(stu.getScore()));
          serializer.endTag(null, "score");   
          serializer.endTag(null, "student");
          serializer.endTag(null, "students");
          //写入结束文档
          serializer.endDocument();
       }
    
    }
    
     
    
  • 相关阅读:
    [转]Linux中用编译的Zlib库替换系统自带的
    [转]Leptonica在VS2010中的编译及简单使用举例
    我的tesseract学习记录
    [转]在VS2010下编译和使用tesseract_ocr
    [转]图像resize
    JVM基础知识(摘抄整理)
    JVM运行时数据内存区和指令集(摘抄整理)
    JMM For Object Size(摘抄整理)
    JMM课程小结(摘抄整理)
    Class的加载过程
  • 原文地址:https://www.cnblogs.com/loaderman/p/6414982.html
Copyright © 2011-2022 走看看