Java实现XML文件学生通讯录

基于XML的同学录(xsl样式表) 主要实现了基于XML的同学录,根据XML文档,用DTD检验及保证其有效性,使其结构完整良好,编写xsl文档,使其按照规定的样表显示,并运用XPath、Xlink、Xpointer等实现向HTML的转换,最后显示在浏览器上。直观,简单,方便。 立即下载

java实现学生通讯录

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

}
/*
查找
*/

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;i<persons.size();i++) {
                    Element test=(Element)persons.get(i);
                    Person person1=new Person();
                    person1.setId(test.attributeValue("id"));
                    person1.setName(test.elementText("name"));
                    person1.setSex(test.elementText("sex"));
                    person1.setAge(test.elementText("age"));
                    person1.setPhone(test.elementText("phone"));
                    person1.setEmail(test.elementText("email"));
                    person1.setBirthday(test.elementText("birthday"));
                    person1.setAddress(test.elementText("address"));
                    person1.setMajor(test.elementText("major"));
                    Object[] rowData = {person1.getId(), person1.getName(),person1.getSex(),person1.getAge(), person1.getBirthday(), person1.getPhone(), person1.getEmail(),person1.getAddress(),person1.getMajor()};
                    model.addRow(rowData);
                }
                    radioButton_4.setSelected(true);
            }

        });
        radioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                Document doc = XML.getDocument();
                Element root=doc.getRootElement();
                List persons=root.elements();
                String str=radioButton.getText();
                Person person1=new Person();
                DefaultTableModel model = (DefaultTableModel) table.getModel();
                while(model.getRowCount()>1){
                     model.removeRow(model.getRowCount()-1);
                }
                for(int i=0;i<persons.size();i++) {
                    Element test =(Element)persons.get(i);
                        if(str.equals(test.element("major").getText())) {
                            person1.setId(test.attributeValue("id"));
                            person1.setName(test.elementText("name"));
                            person1.setSex(test.elementText("sex"));
                            person1.setAge(test.elementText("age"));
                            person1.setPhone(test.elementText("phone"));
                            person1.setEmail(test.elementText("email"));
                            person1.setBirthday(test.elementText("birthday"));
                            person1.setAddress(test.elementText("address"));
                            person1.setMajor(test.elementText("major"));
                        Object[] rowData = {person1.getId(), person1.getName(),person1.getSex(),person1.getAge(), person1.getBirthday(), person1.getPhone(), person1.getEmail(),person1.getAddress(),person1.getMajor()};
                        model.addRow(rowData);
                        }
                }

            }
        });
        radioButton_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                Document doc = XML.getDocument();
                Element root=doc.getRootElement();
                List persons=root.elements();
                String str=radioButton_1.getText();
                Person person1=new Person();
                DefaultTableModel model = (DefaultTableModel) table.getModel();
                while(model.getRowCount()>1){
                     model.removeRow(model.getRowCount()-1);
                }
                for(int i=0;i<persons.size();i++) {
                    Element test =(Element)persons.get(i);
                        if(str.equals(test.element("major").getText())) {
                            person1.setId(test.attributeValue("id"));
                            person1.setName(test.elementText("name"));
                            person1.setSex(test.elementText("sex"));
                            person1.setAge(test.elementText("age"));
                            person1.setPhone(test.elementText("phone"));
                            person1.setEmail(test.elementText("email"));
                            person1.setBirthday(test.elementText("birthday"));
                            person1.setAddress(test.elementText("address"));
                            person1.setMajor(test.elementText("major"));
                        Object[] rowData = {person1.getId(), person1.getName(),person1.getSex(),person1.getAge(), person1.getBirthday(), person1.getPhone(), person1.getEmail(),person1.getAddress(),person1.getMajor()};
                        model.addRow(rowData);
                        }
                }

            }
        });
        radioButton_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                Document doc = XML.getDocument();
                Element root=doc.getRootElement();
                List persons=root.elements();
                String str=radioButton_2.getText();
                Person person1=new Person();
                DefaultTableModel model = (DefaultTableModel) table.getModel();
                while(model.getRowCount()>1){
                     model.removeRow(model.getRowCount()-1);
                }
                for(int i=0;i<persons.size();i++) {
                    Element test =(Element)persons.get(i);
                        if(str.equals(test.element("major").getText())) {
                            person1.setId(test.attributeValue("id"));
                            person1.setName(test.elementText("name"));
                            person1.setSex(test.elementText("sex"));
                            person1.setAge(test.elementText("age"));
                            person1.setPhone(test.elementText("phone"));
                            person1.setEmail(test.elementText("email"));
                            person1.setBirthday(test.elementText("birthday"));
                            person1.setAddress(test.elementText("address"));
                            person1.setMajor(test.elementText("major"));

                        Object[] rowData = {person1.getId(), person1.getName(),person1.getSex(),person1.getAge(), person1.getBirthday(), person1.getPhone(), person1.getEmail(),person1.getAddress(),person1.getMajor()};
                        model.addRow(rowData);
                        }
                }
            }
        });
        radioButton_3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                Document doc = XML.getDocument();
                Element root=doc.getRootElement();
                List persons=root.elements();
                String str=radioButton_3.getText();
                Person person1=new Person();
                DefaultTableModel model = (DefaultTableModel) table.getModel();
                while(model.getRowCount()>1){
                     model.removeRow(model.getRowCount()-1);
                }
                for(int i=0;i<persons.size();i++) {
                    Element test =(Element)persons.get(i);
                        if(str.equals(test.element("major").getText())) {
                            person1.setId(test.attributeValue("id"));
                            person1.setName(test.elementText("name"));
                            person1.setSex(test.elementText("sex"));
                            person1.setAge(test.elementText("age"));
                            person1.setPhone(test.elementText("phone"));
                            person1.setEmail(test.elementText("email"));
                            person1.setBirthday(test.elementText("birthday"));
                            person1.setAddress(test.elementText("address"));
                            person1.setMajor(test.elementText("major"));

                        Object[] rowData = {person1.getId(), person1.getName(),person1.getSex(),person1.getAge(), person1.getBirthday(), person1.getPhone(), person1.getEmail(),person1.getAddress(),person1.getMajor()};
                        model.addRow(rowData);
                        }
                }
            }
        });
    }
    public static void main(String[] args) {
        MainContral test1=new MainContral();
           test1.setVisible(true);
    }
}
基于XML的通讯录 XML通讯录包含:联系人的添加于删除,联系人的修改,联系人查看于管理; 立即下载

相关推荐

java和xml学生通讯录

完整的java程序,包含xml下载即可运行。如需要word文档可联系

Java解析XML

无

qq_64388611的博客 1346

XML通讯录(用java实现)

用java实现的一个简单的通讯录,有 完整的代码

XML文件作业---实现简单的通讯录功能(XML作为简单数据库)(总结)

XML文件作业---实现简单的通讯录功能(XML作为简单数据库) 实现功能:对XML文件进行信息的增删查改; 项目整体视图: 对象类:Contact.java package Test; /* * 联系人实体对象 */ public class Contact { private String id; private String name; private String g

yanghui07216的博客 6610

计算机考研常见面试题

写在前面 本人参加20考研,由于复试没有具体要求会问哪几门课,所以我就找了网上一些常见的计算机专业的面试题,整理如下 主要结构: 数据库 软件工程 操作系统 计算机组成原理 计算机网络 数据结构 C++ Java 数据库 1、数据:数据库中存储的基本对象。 数据库:长期存储在计算机内的、有组织的、可共享的数据集合。 数据库管理系统:专门用于建立和管理数据库的一套软件。对外部程序提供访问数据库的方法,对内进行数据管理。 数据库系统:与和数据库相关的整个系统,一般由数据库、数据库管理系统、应用程序、数

qq_38841618的博客 2万+

Imatest-Star模块(西门子星图)

星图分析指南:基于ISO12233标准的MTF测试方法,通过放射状射线和正弦灰度变化评估相机SFR性能。

weixin_64706696的博客 1566

java解析通讯录xml_android下通过xstream解析复杂的xml格式信息

在android下通过xstream解析xml格式信息一文中介绍了通过xstream解析比较简单的xml文件到相应的Java bean中,下面介绍带节点的xml文件,xml文件格式如下ShangHai上海ShangHai ...上海...解析的代码如下:1、先创建相应的java bean:public class CityInfo {private String id;private List n...

weixin_39634443的博客 165

飞傲Fiio+foobar2000 实现dsd解码+灯正常显示

飞傲Fiio+foobar2000 实现dsd解码+灯正常显示。安装后可将DSD格式的数据通过ASIO驱动传输至DAC。安装后可识别DAC的ASIO驱动。安装后可识别DSD文件。

yanlei7993的博客 2301

Java对XML的解析

XML解析

SevenyLoveWmy 381

Foxmail 7.2.25以下版本XSS漏洞实战复现:手把手教你如何构造恶意邮件(含防御方案)

本文深入解析Foxmail 7.2.25以下版本的XSS漏洞(CNVD-2025-06036),详细展示如何构造恶意邮件进行跨站脚本攻击,并提供从客户端升级到企业级防护的多层次防御方案,帮助用户有效防范安全风险。

view3的博客 163

从重复劳动到智能闭环:OpenClaw实战搭建小红书&公众号全自动化运营体系

如果你有一定的代码开发能力,还可以为OpenClaw开发自定义插件,拓展更多的功能,比如对接更多的平台,实现图文+短视频的全平台自动化运营;对接视频剪辑工具,实现短视频脚本生成、剪辑、配音、发布的全流程自动化;对接私域运营工具,实现公域引流、私域承接、用户转化的全链路自动化;对接电商平台,实现内容种草、商品上架、订单管理的全流程自动化,打造完整的内容电商闭环。新媒体运营的本质,是内容价值的传递,而不是机械的重复劳动。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 311

xml对java通讯录的解析_Dom4j解析xml格式通讯录文档

JDBC连接数据库,进行DOM4j解析的类:AnalyzeXML.Javapackagexml;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.sql.*;importjava.util.Iterator;importjava.util.Li...

weixin_39887846的博客 365

TriCore架构-基于TC397的启动流程

TC397芯片的启动流程分为三个关键阶段:固件启动(BootFirmware)、Bootloader引导和应用程序启动。BootFirmware作为硬件信任根,包含SSW基础初始化、CHSW自检和BSL应急刷写通道。Bootloader承担安全验证和程序引导功能,支持双分区(AB-SWAP)和双引导方案(PBL/SBL)。安全启动通过校验机制确保软件完整性,提供三种可选技术方案。应用程序启动阶段实现软件双分区管理,支持SOTA更新。整个流程体现了汽车电子系统的安全设计理念,通过硬件级保护与软件验证相结合确保

xiandang8023的博客 987

java学生通讯录_Java实现XML文件学生通讯录

java实现学生通讯录,使用XML文件作为数据存储,实现学生编号,姓名,年龄,住址,专业,生日等信息的添加,删除,修改,查找,备份,恢复,专业分类查找等功能。项目需要导入dom4j.jar包。/*添加学生记录*/import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.ButtonGroup;import ...

weixin_39561431的博客 492

【AVRCP】规范精讲[19]: 媒体属性全解析,让你的蓝牙设备显示完整歌曲信息

AVRCP协议中的媒体属性是蓝牙音频设备传输歌曲元数据的关键,包含标题、艺术家、专辑等32位ID标识的键值对信息。规范要求字符串属性必须使用UTF-8编码,但乱码问题频发(如中文显示异常),主因是设备未遵循编码标准或使用GBK等非规范格式。常见问题包括信息不全(仅显示标题)、封面缺失(因带宽限制或格式不支持)及更新延迟(通知机制未启用)。解决方案包括强制UTF-8编码、优化封面大小(如300x300像素JPEG)、主动请求缺失属性。Android示例代码展示了通过广播接收器解析媒体属性的方法。

byte轻骑兵的技术小窝 1万+

Qt应用软件【协议篇】自定义协议处理串口半包贴包

Qt串口数据,半包,贴包,丢包数据处理

小鱼酱的博客 1211

【附源码】java毕业设计通讯录管理系统

这样,前端Vue.js和后端Spring Boot框架的接口紧密协作,而MySQL数据库则作为数据存储和管理的核心,确保了整个系统的数据一致性和高效处理。其次,系统可以为用户提供多种数据备份和恢复方案,确保用户数据的完整性和安全性。本毕业设计旨在利用html、css、javascript和vue等前端技术,结合java、springboot后端技术和mysql数据库,通过eclipse或idea开发工具,配合tomcat服务器、jdk运行环境和maven项目管理工具,打造一个现代化的通讯录管理平台。

基努毕设程序 806

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

绿豆通讯录源码:Android实战案例

绿豆通讯录源码:Android实战案例 【下载地址】Android实战案例绿豆通讯录源码 绿豆通讯录是一款基于Android Studio开发的实战案例应用,专为初学者设计,帮助理解Android应用开发的核心技术。该项目涵盖了界面布局、数据存储、事件处理等基础知识,通过完整源码和详细教程,用户可以快速上手并掌握开发流程...

gitblog_06712的博客 386

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

java课程设计

<!-- @page { size: 21cm 29.7cm; margin: 2cm } H1 { margin-top: 0.6cm; margin-bottom: 0.58cm; line-height: 200%; page-break-inside: avoid } H1.western { font-family: "AR PL UMing C

Dill的专栏 5462

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

java_ssm5个人博客日志通讯录通信簿2qwac

本系统采取了一系列的设计原则,主要目的是为了系统的功能设计,还有管理人员在后期对系统维护时的方便,以及使学生能够简易的操作。最重要的设计原则包括:简单性、针对性、实用性、一致性、先进性。网站整体的页面布局,在不同的界面之间,img里的图片的放置位置以及大小都应该有严格的一致性。变量命名规则应该具有统一性1、学习系统开发和设计的技术相关知识和工作流程;2、学习使用 IDEA 工具编辑前后台代码;3、学习使用springboot-ssm框架实现系统的开发;

QQ402205496的博客 1158

springboot_ssm个人博客日志通讯录通信簿2qwac

本系统采取了一系列的设计原则,主要目的是为了系统的功能设计,还有管理人员在后期对系统维护时的方便,以及使学生能够简易的操作。最重要的设计原则包括:简单性、针对性、实用性、一致性、先进性。网站整体的页面布局,在不同的界面之间,img里的图片的放置位置以及大小都应该有严格的一致性。变量命名规则应该具有统一性1、学习系统开发和设计的技术相关知识和工作流程;2、学习使用 IDEA 工具编辑前后台代码;3、学习使用springboot-ssm框架实现系统的开发;

bishe288的博客 673

XML

XML入门引入 HTML: 负责网页的结构 CSS: 负责网页的样式(美观) Javascript: 负责在浏览器端与用户进行交互。 负责静态的网页制作的语言 HTML语言特点: 1)由标签组成。 <title> <p> <hr/> <br/> 2)语法结构松散的 <p></p> <p> <P>

gafeng123456的博客 356

1.xml文件的基础语法知识

1 XML入门 2.1 回顾 html: 主要负责网页的结构。 css:主要负责网页的美观/样式。 javascript: 主要负责在浏览器端与客户进行交互。 以上三门语言都是静态网页制作的语言。 html语言特点: 1)由标签组成。&lt;p&gt;&lt;/p&gt; &lt;br...

Web小笨猪的博客 336

javaEE__xml

1 XML入门 1.1 引入 HTML: 负责网页的结构 CSS: 负责网页的样式(美观) Javascript: 负责在浏览器端与用户进行交互。 负责静态的网页制作的语言 HTML语言特点: 1)由标签组成。 <title> <p> <hr/> <br/>

小鱼儿 601

xml作用以及语法

2 XML作用 2.1 描述带关系的数据(软件的配置文件) web服务器(PC): 学生管理系统 -> 添加学生功能 -> 添加学生页面 -> name=eric&email=eric@qq.com 前提: 网络(IP地址: oracle:255.43.12.54 端口:1521 ) java代码:使用ip(255.43.12.54)地址和端口(1521)

态度决定高度A=X+Y+Z 533
上一篇: Java实现多文档文本编辑器
下一篇: 如何用git将项目代码上传到github
刺破黎明
博客等级 码龄9年 42粉丝 12原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值