集册 Java实例教程 从XML反序列化Bean

从XML反序列化Bean

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

626
从XML反序列化Bean



import java.beans.XMLDecoder;
/** from 
时 代 J a v a 公 众 号 - nowjava.com**/

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;


public class Main {

  public static void main(String[] args) throws Exception {

    try {

      XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(

          new FileInputStream("infilename.xml")));


      MyClass o = (MyClass) decoder.readObject();

      decoder.close();


      // Use the object

      int prop = o.getProp(); // 1/*时代Java*/

      int[] props = o.getProps(); // [1, 2, 3]

    } catch (FileNotFoundException e) {

    }

  }

}


class MyClass{

  int prop;

  int[] props;

  public int getProp() {

    return prop;

  }

  public void setProp(int prop) {

    this.prop = prop;

  }

  
展开阅读全文