服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

创建并解析xml文件java实例

/* 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;
    }
   
 


}

扫描关注微信公众号