Java实现XML文件学生通讯录

这篇文章主要为大家详细介绍了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, 5)); contentPane.setLayout(null); setContentPane(contentPane); JLabel jl1 = new JLabel("学生通讯录"); jl1.setFont(new Font("宋体", Font.PLAIN, 16)); jl1.setBounds(10, 0, 95, 29); contentPane.add(jl1); JLabel jl2 = new JLabel("学生编号"); jl2.setFont(new Font("宋体", Font.PLAIN, 16)); jl2.setBounds(51, 39, 65, 29); contentPane.add(jl2); JLabel jl3 = new JLabel("学生姓名"); jl3.setFont(new Font("宋体", Font.PLAIN, 16)); jl3.setBounds(51, 78, 65, 29); contentPane.add(jl3); JLabel jl4 = new JLabel("学生性别"); jl4.setFont(new Font("宋体", Font.PLAIN, 16)); jl4.setBounds(51, 117, 65, 29); contentPane.add(jl4); JLabel jl5 = new JLabel("学生年龄"); jl5.setFont(new Font("宋体", Font.PLAIN, 16)); jl5.setBounds(51, 163, 65, 29); contentPane.add(jl5); JLabel jl6 = new JLabel("出生日期"); jl6.setFont(new Font("宋体", Font.PLAIN, 16)); jl6.setBounds(51, 202, 65, 29); contentPane.add(jl6); JLabel jl7 = new JLabel("手机号码"); jl7.setFont(new Font("宋体", Font.PLAIN, 16)); jl7.setBounds(51, 250, 65, 29); contentPane.add(jl7); JLabel jl8 = new JLabel("电子邮箱"); jl8.setFont(new Font("宋体", Font.PLAIN, 16)); jl8.setBounds(51, 296, 65, 29); contentPane.add(jl8); JLabel jl9 = new JLabel("家庭住址"); jl9.setFont(new Font("宋体", Font.PLAIN, 16)); jl9.setBounds(51, 345, 65, 29); contentPane.add(jl9); JLabel jl10 = new JLabel("专业名称"); jl10.setFont(new Font("宋体", Font.PLAIN, 16)); jl10.setBounds(51, 389, 65, 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, 150, 34); contentPane.add(jt2); jt3 = new JTextField(); jt3.setColumns(10); jt3.setBounds(129, 163, 150, 34); contentPane.add(jt3); jt4 = new JTextField(); jt4.setColumns(10); jt4.setBounds(129, 201, 150, 34); contentPane.add(jt4); jt5 = new JTextField(); jt5.setColumns(10); jt5.setBounds(129, 250, 150, 34); contentPane.add(jt5); jt6 = new JTextField(); jt6.setColumns(10); jt6.setBounds(129, 301, 150, 34); contentPane.add(jt6); jt7 = new JTextField(); jt7.setColumns(10); jt7.setBounds(129, 350, 150, 34); contentPane.add(jt7); JRadioButton radioButton = new JRadioButton("男",true); radioButton.setBounds(139, 121, 65, 23); contentPane.add(radioButton); JRadioButton radioButton_1 = new JRadioButton("女"); radioButton_1.setBounds(221, 121, 121, 23); contentPane.add(radioButton_1); ButtonGroup bb=new ButtonGroup(); bb.add(radioButton);bb.add(radioButton_1); JButton jb1 = new JButton("添加"); jb1.setFont(new Font("宋体", Font.PLAIN, 16)); jb1.setBounds(314, 78, 110, 61); contentPane.add(jb1); JButton jb2 = new JButton("关闭"); jb2.setFont(new Font("宋体", Font.PLAIN, 16)); jb2.setBounds(314, 167, 110, 61); contentPane.add(jb2); JButton jb3 = new JButton("清除"); jb3.setFont(new Font("宋体", Font.PLAIN, 16)); jb3.setBounds(314, 265, 110, 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

 /* 查找 */ 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 SerachPerson 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 SerachPerson() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 489); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(null); setContentPane(contentPane); JLabel jl1 = new JLabel("学生通讯录"); jl1.setFont(new Font("宋体", Font.PLAIN, 16)); jl1.setBounds(10, 0, 95, 29); contentPane.add(jl1); JLabel jl2 = new JLabel("学生编号"); jl2.setFont(new Font("宋体", Font.PLAIN, 16)); jl2.setBounds(51, 39, 65, 29); contentPane.add(jl2); JLabel jl3 = new JLabel("学生姓名"); jl3.setFont(new Font("宋体", Font.PLAIN, 16)); jl3.setBounds(51, 78, 65, 29); contentPane.add(jl3); JLabel jl4 = new JLabel("学生性别"); jl4.setFont(new Font("宋体", Font.PLAIN, 16)); jl4.setBounds(51, 117, 65, 29); contentPane.add(jl4); JLabel jl5 = new JLabel("学生年龄"); jl5.setFont(new Font("宋体", Font.PLAIN, 16)); jl5.setBounds(51, 163, 65, 29); contentPane.add(jl5); JLabel jl6 = new JLabel("出生日期"); jl6.setFont(new Font("宋体", Font.PLAIN, 16)); jl6.setBounds(51, 202, 65, 29); contentPane.add(jl6); JLabel jl7 = new JLabel("手机号码"); jl7.setFont(new Font("宋体", Font.PLAIN, 16)); jl7.setBounds(51, 250, 65, 29); contentPane.add(jl7); JLabel jl8 = new JLabel("电子邮箱"); jl8.setFont(new Font("宋体", Font.PLAIN, 16)); jl8.setBounds(51, 296, 65, 29); contentPane.add(jl8); JLabel jl9 = new JLabel("家庭住址"); jl9.setFont(new Font("宋体", Font.PLAIN, 16)); jl9.setBounds(51, 345, 65, 29); contentPane.add(jl9); JLabel jl10 = new JLabel("专业名称"); jl10.setFont(new Font("宋体", Font.PLAIN, 16)); jl10.setBounds(51, 389, 65, 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, 150, 34); contentPane.add(jt2); jt3 = new JTextField(); jt3.setColumns(10); jt3.setBounds(129, 163, 150, 34); contentPane.add(jt3); jt4 = new JTextField(); jt4.setColumns(10); jt4.setBounds(129, 201, 150, 34); contentPane.add(jt4); jt5 = new JTextField(); jt5.setColumns(10); jt5.setBounds(129, 250, 150, 34); contentPane.add(jt5); jt6 = new JTextField(); jt6.setColumns(10); jt6.setBounds(129, 301, 150, 34); contentPane.add(jt6); jt7 = new JTextField(); jt7.setColumns(10); jt7.setBounds(129, 350, 150, 34); contentPane.add(jt7); JRadioButton radioButton = new JRadioButton("男",true); radioButton.setBounds(139, 121, 65, 23); contentPane.add(radioButton); JRadioButton radioButton_1 = new JRadioButton("女"); radioButton_1.setBounds(221, 121, 79, 23); contentPane.add(radioButton_1); ButtonGroup bb=new ButtonGroup(); bb.add(radioButton);bb.add(radioButton_1); JButton jb1 = new JButton("查找"); jb1.setFont(new Font("宋体", Font.PLAIN, 16)); jb1.setBounds(314, 78, 110, 61); contentPane.add(jb1); JButton jb2 = new JButton("关闭"); jb2.setFont(new Font("宋体", Font.PLAIN, 16)); jb2.setBounds(314, 280, 110, 61); contentPane.add(jb2); JButton jb3 = new JButton("清除"); jb3.setFont(new Font("宋体", Font.PLAIN, 16)); jb3.setBounds(314, 357, 110, 61); contentPane.add(jb3); JButton jb4 = new JButton("修改"); jb4.setFont(new Font("宋体", Font.PLAIN, 16)); jb4.setBounds(314, 174, 110, 61); contentPane.add(jb4); String str1[]= {"计算机科学与技术","信息安全","电子信息科学与技术","网络工程"}; JComboBox comboBox = new JComboBox(str1); comboBox.setBounds(126, 394, 153, 29); contentPane.add(comboBox); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { PersonOperatorDome person=new PersonOperatorDome(); String id=jt1.getText(); Person pp=person.SerachPerson(id); jt2.setText(pp.getName()); if(radioButton.getText().equals(pp.getSex())) { radioButton.setSelected(true); }else { radioButton_1.setSelected(true); } jt3.setText(pp.getAge()); jt4.setText(pp.getBirthday()); jt5.setText(pp.getPhone()); jt6.setText(pp.getEmail()); jt7.setText(pp.getAddress()); if(pp.getMajor().equals("计算机科学与技术")) comboBox.setSelectedIndex(0); if(pp.getMajor().equals("信息安全")) comboBox.setSelectedIndex(1); if(pp.getMajor().equals("电子信息科学与技术")) comboBox.setSelectedIndex(2); if(pp.getMajor().equals("网络工程")) comboBox.setSelectedIndex(3); } }); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 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(); } }); jb4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { 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.UpdataPerson(pp); JOptionPane.showMessageDialog(null, "修改成功"); }}); } } 
 import java.io.File; import java.io.FileOutputStream; import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class XML { public static Document getDocument() { try { Document doc = new SAXReader().read(new File("e:/person.xml")); return doc; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public static Document getDocument1() { try { Document doc = new SAXReader().read(new File("e:/persondome.xml")); return doc; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /* * 写出XML文件 */ public static void Writexml(Document doc) { try { //3.把Document写到XML文件 FileOutputStream out = new FileOutputStream("e:/person.xml"); //书写格式(优美的格式) OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter writer = new XMLWriter(out,format); writer.write(doc); writer.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } } 

 import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.ButtonGroup; import javax.swing.JButton; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JTextArea; import javax.swing.JScrollBar; import javax.swing.JScrollPane; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JRadioButton; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import org.dom4j.Document; import org.dom4j.Element; public class MainContral extends JFrame { private JPanel contentPane; private JTable table; public MainContral() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 1200, 392); contentPane = new JPanel(); contentPane.setLayout(null); setContentPane(contentPane); JButton jb1 = new JButton("添加"); jb1.setFont(new Font("宋体", Font.PLAIN, 16)); jb1.setBounds(23, 10, 101, 48); contentPane.add(jb1); JButton jb3 = new JButton("删除"); jb3.setFont(new Font("宋体", Font.PLAIN, 16)); jb3.setBounds(134, 10, 101, 48); contentPane.add(jb3); JButton jb4 = new JButton("\u67E5\u627E\u4E0E\u4FEE\u6539"); jb4.setFont(new Font("宋体", Font.PLAIN, 16)); jb4.setBounds(245, 10, 139, 48); contentPane.add(jb4); JButton jb5 = new JButton("显示全部"); jb5.setFont(new Font("宋体", Font.PLAIN, 16)); jb5.setBounds(394, 10, 126, 48); contentPane.add(jb5); JRadioButton radioButton = new JRadioButton("计算机科学与技术"); radioButton.setFont(new Font("宋体", Font.PLAIN, 16)); radioButton.setBounds(23, 89, 161, 23); contentPane.add(radioButton); JRadioButton radioButton_1 = new JRadioButton("信息安全"); radioButton_1.setFont(new Font("宋体", Font.PLAIN, 16)); radioButton_1.setBounds(186, 89, 101, 23); contentPane.add(radioButton_1); JRadioButton radioButton_2 = new JRadioButton("电子信息科学与技术"); radioButton_2.setFont(new Font("宋体", Font.PLAIN, 16)); radioButton_2.setBounds(289, 89, 174, 23); contentPane.add(radioButton_2); JRadioButton radioButton_3 = new JRadioButton("网络工程"); radioButton_3.setFont(new Font("宋体", Font.PLAIN, 16)); radioButton_3.setBounds(465, 89, 101, 23); contentPane.add(radioButton_3); JRadioButton radioButton_4 = new JRadioButton("全部"); radioButton_4.setFont(new Font("宋体", Font.PLAIN, 16)); radioButton_4.setBounds(568, 89, 121, 23); contentPane.add(radioButton_4); ButtonGroup bb=new ButtonGroup(); bb.add(radioButton); bb.add(radioButton_3); bb.add(radioButton_2); bb.add(radioButton_1); bb.add(radioButton_4); table = new JTable(); table.setFont(new Font("宋体", Font.PLAIN, 16)); table.setModel(new DefaultTableModel( new Object[][] { }, new String[] { "\u7F16\u53F7", "\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u751F\u65E5", "\u7535\u8BDD", "\u90AE\u7BB1", "\u5730\u5740", "\u4E13\u4E1A" } )); DefaultTableModel model = (DefaultTableModel) table.getModel(); Object[] rowData= {"编号","姓名","性别","年龄","生日","电话","邮箱","地址","专业"}; model.addRow(rowData); //JScrollPane scroll = new JScrollPane(table); table.setBounds(23, 131, 1100, 212); contentPane.add(table); JButton jb6 = new JButton("备份"); jb6.setFont(new Font("宋体", Font.PLAIN, 16)); jb6.setBounds(530, 10, 101, 48); contentPane.add(jb6); JButton jb7 = new JButton("恢复"); jb7.setFont(new Font("宋体", Font.PLAIN, 16)); jb7.setBounds(641, 10, 101, 48); contentPane.add(jb7); jb6.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub PersonOperatorDome p=new PersonOperatorDome(); p.beifen(); JOptionPane.showMessageDialog(null, "备份成功"); } }); jb7.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub PersonOperatorDome p=new PersonOperatorDome(); p.huifu(); JOptionPane.showMessageDialog(null, "恢复成功"); } }); jb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub AddPerson test=new AddPerson(); test.setVisible(true); } }); jb3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub int count1=table.getSelectedRow(); String id= table.getValueAt(count1, 0).toString(); PersonOperatorDome p=new PersonOperatorDome(); p.deletePerson(id); JOptionPane.showMessageDialog(null, "删除成功"); } }); jb4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub SerachPerson test=new SerachPerson(); test.setVisible(true); } }); jb5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub PersonOperatorDome person=new PersonOperatorDome(); Document doc = XML.getDocument(); Element root=doc.getRootElement(); List persons=root.elements(); DefaultTableModel model = (DefaultTableModel) table.getModel(); while(model.getRowCount()>1){ model.removeRow(model.getRowCount()-1); } for(int i=0;i1){ model.removeRow(model.getRowCount()-1); } for(int i=0;i1){ model.removeRow(model.getRowCount()-1); } for(int i=0;i1){ model.removeRow(model.getRowCount()-1); } for(int i=0;i1){ model.removeRow(model.getRowCount()-1); } for(int i=0;i

以上就是Java实现XML文件学生通讯录的详细内容,更多请关注html中文网其它相关文章!

赞(0) 打赏
未经允许不得转载:html中文网首页 » Java

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏