/* xmltest.java
* created on 2004-11-16
*/
package test;
import java.beans.xmlencoder;
import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.fileinputstream;
import java.io.fileoutputstream;
public class xmltest {
public void xmlencode() throws exception
{
myinfo my = new myinfo();
my.setmyage(25);
my.setmyname("google");
my.setmyaddress("china");
my.setmyeducation("master in science");
xmlencoder encoder = new xmlencoder(
new bufferedoutputstream(
new fileoutputstream("myinfo.xml")));
encoder.writeobject(my);
encoder.close();
system.out.println(my);
}
public void xmldecode() throws exception
{
java.beans.xmldecoder decoder = new java.beans.xmldecoder(
new bufferedinputstream(new fileinputstream("myinfo.xml")));
myinfo my = (myinfo)decoder.readobject();
decoder.close();
system.out.println(my);
system.out.println("your age: "+my.getmyage());
system.out.println("your name: "+my.getmyname());
system.out.println("your address: "+my.getmyaddress());
system.out.println("your education: "+my.getmyeducation());
}
public static void main (string args[]) throws exception {
xmltest st = new xmltest();
st.xmlencode();
st.xmldecode();
}
}
对应的辅助类myinfo代码如下:
package test;
/**
* add one sentence class summary here.
* add class description here.
*
* @author lxx
* @version 1.0, 2004-11-16
*/
public class myinfo {
private int myage;
private string myname;
private string myaddress;
private string myeducation;
public myinfo(){ }
public int getmyage (){
return myage;
}
public void setmyage (int age){
this.myage=age;
}
public string getmyname() {
return myname;
}
public void setmyname(string name) {
this.myname=name;
}
public string getmyaddress() {
return myaddress;
}
public void setmyaddress(string address) {
this.myaddress=address;
}
public string getmyeducation() {
return myeducation;
}
public void setmyeducation (string education){
this.myeducation=education;
}
}
闽公网安备 35060202000074号