加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java实现XML文件学生通讯录

发布时间:2020-05-25 09:15:09 所属栏目:Java 来源:互联网
导读:java实现学生通讯录,使用XML文件作为数据存储,实现学生编号,姓名,年龄,住址,专业,生日等信息的添加,删除,修改,查找,备份,恢复,专业分类查找等功能。

java实现学生通讯录,使用XML文件作为数据存储,实现学生编号,姓名,年龄,住址,专业,生日等信息的添加,删除,修改,查找,备份,恢复,专业分类查找等功能。

项目需要导入dom4j.jar包。

/*
添加学生记录
*/

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
public class AddPerson extends JFrame {
  private JPanel contentPane;
  private JTextField jt1;
  private JTextField jt2;
  private JTextField jt3;
  private JTextField jt4;
  private JTextField jt5;
  private JTextField jt6;
  private JTextField jt7;
  public AddPerson() {
    setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100,100,450,489);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5,5,5));
    contentPane.setLayout(null);
    setContentPane(contentPane);

    JLabel jl1 = new JLabel("学生通讯录");
    jl1.setFont(new Font("宋体",Font.PLAIN,16));
    jl1.setBounds(10,95,29);
    contentPane.add(jl1);

    JLabel jl2 = new JLabel("学生编号");
    jl2.setFont(new Font("宋体",16));
    jl2.setBounds(51,39,65,29);
    contentPane.add(jl2);

    JLabel jl3 = new JLabel("学生姓名");
    jl3.setFont(new Font("宋体",16));
    jl3.setBounds(51,78,29);
    contentPane.add(jl3);

    JLabel jl4 = new JLabel("学生性别");
    jl4.setFont(new Font("宋体",16));
    jl4.setBounds(51,117,29);
    contentPane.add(jl4);

    JLabel jl5 = new JLabel("学生年龄");
    jl5.setFont(new Font("宋体",16));
    jl5.setBounds(51,163,29);
    contentPane.add(jl5);

    JLabel jl6 = new JLabel("出生日期");
    jl6.setFont(new Font("宋体",16));
    jl6.setBounds(51,202,29);
    contentPane.add(jl6);

    JLabel jl7 = new JLabel("手机号码");
    jl7.setFont(new Font("宋体",16));
    jl7.setBounds(51,250,29);
    contentPane.add(jl7);

    JLabel jl8 = new JLabel("电子邮箱");
    jl8.setFont(new Font("宋体",16));
    jl8.setBounds(51,296,29);
    contentPane.add(jl8);

    JLabel jl9 = new JLabel("家庭住址");
    jl9.setFont(new Font("宋体",16));
    jl9.setBounds(51,345,29);
    contentPane.add(jl9);

    JLabel jl10 = new JLabel("专业名称");
    jl10.setFont(new Font("宋体",16));
    jl10.setBounds(51,389,29);
    contentPane.add(jl10);

    jt1 = new JTextField();
    jt1.setBounds(129,38,150,34);
    contentPane.add(jt1);
    jt1.setColumns(10);

    jt2 = new JTextField();
    jt2.setColumns(10);
    jt2.setBounds(129,83,34);
    contentPane.add(jt2);

    jt3 = new JTextField();
    jt3.setColumns(10);
    jt3.setBounds(129,34);
    contentPane.add(jt3);

    jt4 = new JTextField();
    jt4.setColumns(10);
    jt4.setBounds(129,201,34);
    contentPane.add(jt4);

    jt5 = new JTextField();
    jt5.setColumns(10);
    jt5.setBounds(129,34);
    contentPane.add(jt5);

    jt6 = new JTextField();
    jt6.setColumns(10);
    jt6.setBounds(129,301,34);
    contentPane.add(jt6);

    jt7 = new JTextField();
    jt7.setColumns(10);
    jt7.setBounds(129,350,34);
    contentPane.add(jt7);

    JRadioButton radioButton = new JRadioButton("男",true);
    radioButton.setBounds(139,121,23);
    contentPane.add(radioButton);

    JRadioButton radioButton_1 = new JRadioButton("女");
    radioButton_1.setBounds(221,23);
    contentPane.add(radioButton_1);
    ButtonGroup bb=new ButtonGroup();
    bb.add(radioButton);bb.add(radioButton_1);
JButton jb1 = new JButton("添加");
jb1.setFont(new Font("宋体",16));
jb1.setBounds(314,110,61);
contentPane.add(jb1);
JButton jb2 = new JButton("关闭");
jb2.setFont(new Font("宋体",16));
jb2.setBounds(314,167,61);
contentPane.add(jb2);
JButton jb3 = new JButton("清除");
jb3.setFont(new Font("宋体",16));
jb3.setBounds(314,265,61);
contentPane.add(jb3);

String str1[]= {"计算机科学与技术","信息安全","电子信息科学与技术","网络工程"};
JComboBox comboBox=new JComboBox(str1); ;
comboBox.setBounds(126,400,155,29);
contentPane.add(comboBox);

    jb1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
      PersonOperatorDome ppp=new PersonOperatorDome();
      if(ppp.SerachPersonbool(jt1.getText())) {
        JOptionPane.showMessageDialog(null,"此ID已存在,请勿重复添加");
      }
      else {
        Person pp=new Person();
        PersonOperatorDome person=new PersonOperatorDome();
        String sex;
        if(radioButton.isSelected()) {
           sex=radioButton.getText();
        }
        else {
           sex=radioButton_1.getText();
        }
        pp.setId(jt1.getText());
        pp.setName(jt2.getText());
        pp.setSex(sex);
        pp.setBirthday(jt4.getText());
        pp.setAge(jt3.getText());
        pp.setPhone(jt5.getText());
        pp.setEmail(jt6.getText());
        pp.setAddress(jt7.getText());
        pp.setMajor(String.valueOf(comboBox.getSelectedItem()));
        person.addPerson(pp);
        JOptionPane.showMessageDialog(null,"添加成功");
      }  
    }});
    jb3.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
      // TODO Auto-generated method stub
      jt1.setText("");
      jt2.setText("");
      jt3.setText("");
      jt4.setText("");
      jt5.setText("");
      jt6.setText("");
      jt7.setText("");

    }

    });
    jb2.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
      // TODO Auto-generated method stub
      dispose();
    }
    });
  }
}

/*
人员信息类
*/
public class Person {
  private String id;
  private String name;
  private String sex;
  private String age;
  private String birthday;
  private String phone;
  private String email;
  private String address;
  private String major;
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getSex() {
    return sex;
  }
  public void setSex(String sex) {
    this.sex = sex;
  }
  public String getAge() {
    return age;
  }
  public void setAge(String age) {
    this.age = age;
  }
  public String getBirthday() {
    return birthday;
  }
  public void setBirthday(String birthday) {
    this.birthday = birthday;
  }
  public String getPhone() {
    return phone;
  }
  public void setPhone(String phone) {
    this.phone = phone;
  }
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public String getAddress() {
    return address;
  }
  public void setAddress(String address) {
    this.address = address;
  }
  public String getMajor() {
    return major;
  }
  public void setMajor(String major) {
    this.major = major;
  }

}

/*
人员操作接口
*/
import org.dom4j.Document;

public interface PersonOperator {
  public void addPerson(Person person);
  public void updatePerson(Person person);
  public void deletePerson(String id); 
  public Person SerachPerson(String id);
  public boolean SerachPersonbool(String id);
  public void beifen();
  public void huifu();
}

import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class PersonOperatorDome {
      public void addPerson(Person person) {
        try { 
          File file = new File("E:/person.xml"); 
          Document doc = null; 
          Element rootElem = null; 
          if(!file.exists()) {  
            doc = DocumentHelper.createDocument();  
            rootElem = doc.addElement("personlist"); 
          } else { 
            doc = new SAXReader().read(file); 
            rootElem = doc.getRootElement(); 
          } 
          Element personElem = rootElem.addElement("person"); 
          personElem.addAttribute("id",person.getId()); 
          personElem.addElement("name").setText(person.getName()); 
          personElem.addElement("sex").setText(person.getSex()); 
          personElem.addElement("age").setText(person.getAge()); 
          personElem.addElement("birthday").setText(person.getBirthday()); 
          personElem.addElement("phone").setText(person.getPhone()); 
          personElem.addElement("email").setText(person.getEmail()); 
          personElem.addElement("address").setText(person.getAddress()); 
          personElem.addElement("major").setText(person.getMajor()); 
          XML.Writexml(doc); 
        } catch (Exception e) { 
          e.printStackTrace();
        }  
      } 
    public void UpdataPerson(Person person) { 
    Document doc = XML.getDocument();
    Element root=doc.getRootElement();
    List persons=root.elements();
    for(int i=0;i<persons.size();i++) {
      Element test=(Element)persons.get(i);
      if(person.getId().equals(test.attributeValue("id"))) {
        for(Iterator it =test.elementIterator();it.hasNext();) {
          Element node=(Element)it.next();
          String type=node.getName();
          if("name".equals(type)) {
            node.setText(person.getName());
          }
          if("sex".equals(type)) {
            node.setText(person.getSex());
          }
          if("age".equals(type)) {
            node.setText(person.getAge());
          }
          if("birthday".equals(type)) {
            node.setText(person.getBirthday());
          }
          if("phone".equals(type)) {
            node.setText(person.getPhone());
          }
          if("email".equals(type)) {
            node.setText(person.getEmail());
          }
          if("address".equals(type)) {
            node.setText(person.getAddress());
          }
          if("major".equals(type)) {
            node.setText(person.getMajor());
          }
        }
       }
      }
      XML.Writexml(doc);
    }

    public void beifen() {
      Document doc1 = XML.getDocument();
      try { 
        File file = new File("E:/persondome.xml"); 
        Document doc = null; 
        Element rootElem = null; 
        if(!file.exists()) {  
          doc = DocumentHelper.createDocument();  
          rootElem = doc.addElement("personlist"); 
        } else { 
          doc = new SAXReader().read(file); 
          rootElem = doc.getRootElement(); 
        }
      }catch (Exception e) { 
        e.printStackTrace();
       } 
       try { 
        //3.把Document写到XML文件 
        FileOutputStream out = new FileOutputStream("e:/persondome.xml"); 
        //书写格式(优美的格式) 
        OutputFormat format = OutputFormat.createPrettyPrint(); 
        format.setEncoding("utf-8"); 
        XMLWriter writer = new XMLWriter(out,format); 
        writer.write(doc1); 
        writer.close(); 
      } catch (Exception e) { 
        e.printStackTrace(); 
        throw new RuntimeException(); 
      }  
    }
    public void huifu() {
      Document doc1 = XML.getDocument1();
      try { 
        FileOutputStream out = new FileOutputStream("e:/person.xml"); 
        OutputFormat format = OutputFormat.createPrettyPrint(); 
        format.setEncoding("utf-8"); 
        XMLWriter writer = new XMLWriter(out,format); 
        writer.write(doc1); 
        writer.close(); 
      } catch (Exception e) { 
        e.printStackTrace(); 
        throw new RuntimeException(); 
      }  
    }


    public void deletePerson(String id) { 
    Document doc = XML.getDocument();
    Element root=doc.getRootElement();
    List persons=root.elements();
    for(int i=0;i<persons.size();i++) {
      Element test =(Element)persons.get(i);
      if(id.equals(test.attributeValue("id"))) {
        test.detach();
        break;
      }
    }
    XML.Writexml(doc);
    }

    public boolean SerachPersonbool(String id) {
      Document doc = XML.getDocument();
      Element root=doc.getRootElement();
      List persons=root.elements();
      Person person=new Person();
      for(int i=0;i<persons.size();i++) {
        Element test =(Element)persons.get(i);
        if(id.equals(test.attributeValue("id"))) {
          return true;
        }
      }
      return false;
    }

    public Person SerachPerson(String id) {
    Document doc = XML.getDocument();
    Element root=doc.getRootElement();
    List persons=root.elements();
    Person person=new Person();
    for(int i=0;i<persons.size();i++) {
      Element test =(Element)persons.get(i); 
      if(id.equals(test.attributeValue("id"))) {
        for(Iterator it =test.elementIterator();it.hasNext();) {
          Element node=(Element)it.next();
          String type=node.getName();
          if("name".equals(type)) {
            person.setName(node.getText());
          }
          if("sex".equals(type)) {
            person.setSex(node.getText());
          }
          if("age".equals(type)) {
            person.setAge(node.getText());
          }
          if("birthday".equals(type)) {
            person.setBirthday(node.getText());
          }
          if("phone".equals(type)) {
            person.setPhone(node.getText());
          }
          if("email".equals(type)) {
            person.setEmail(node.getText());
          }
          if("address".equals(type)) {
            person.setAddress(node.getText());
          }
          if("major".equals(type)) {
            person.setMajor(node.getText());
          }
        }
      }
    }
    return person;
    }

}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读