集册 Java实例教程 将Bean序列化为XML

将Bean序列化为XML

欢马劈雪     最近更新时间:2020-01-02 10:19:05

404
将Bean序列化为XML


import java.beans.XMLEncoder;

import java.io.BufferedOutputStream;

import java.io.FileNotFoundException;/** 来 自 时 代 J a v a - nowjava.com**/

import java.io.FileOutputStream;


public class Main {

  public static void main(String[] args) {

    MyClass o = new MyClass();

    o.setProp(1);

    o.setProps(new int[] { 1, 2, 3 });


    try {

      // Serialize object into XML

      XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
      /** 
       来自 nowjava**/

          new FileOutputStream("outfilename.xml")));

      encoder.writeObject(o);

      encoder.close();

    } catch (FileNotFoundException e) {

    }

  }

}


class MyClass {

  // The prop property

  int i;


  public int getProp() {

    return i;

  }


  public void setProp(int i) {

    this.i = i;

  }


  // The props property

  int[] iarray = new 
展开阅读全文