/* 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 ();
}
}
The corresponding helper class MyInfo code is as follows:
Package test;
/**
* Add one sentence class summary here.
* Add class description here.
*
* @ Author lxx
* @ Version 1.0
*/
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;
}
}