xml-dom解析-简单的学生信息管理系统

时间:2021-07-27 14:25:34

xml文档-exam.xml

<?xml version="1.0" encoding="UTF-8"?>
<exam>
     <student examid="111" idcard="111123123">
          <name>张三</name>
          <location>沈阳</location>
          <grade>89</grade>
    </student>
    <student examid="222" idcard="22212342323">
          <name>李四</name>
          <location>大连</location>
          <grade>97</grade>
   </student>
 
</exam>

 

程序实现:完成对exam.xml文档的添加、删除、查询功能 

工具类-XmlUtils

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

 

public class XmlUtils {
 private static File file = new File("src/exam.xml");
 public static Document getDocument() throws ParserConfigurationException, SAXException, IOException{
  //1.获得工厂的实例对象
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  //2.获得DocumentBuilder的实例对象
  DocumentBuilder builder = factory.newDocumentBuilder();
  return builder.parse(file);
 }
 
 public static void write2Xml(Document document) throws TransformerException{
  //1.获得TransformerFactory的实例对象
  TransformerFactory factory = TransformerFactory.newInstance();
  //2.获得Transformer的实例对象
  Transformer tf = factory.newTransformer();
  tf.transform(new DOMSource(document), new StreamResult(file));
 }
}

 

封装数据的Student类

public class Student {
 private String examid;
 private String idcard;
 private String name;
 private String location;
 private double grade;
 public String getExamid() {
  return examid;
 }
 public void setExamid(String examid) {
  this.examid = examid;
 }
 public String getIdcard() {
  return idcard;
 }
 public void setIdcard(String idcard) {
  this.idcard = idcard;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getLocation() {
  return location;
 }
 public void setLocation(String location) {
  this.location = location;
 }
 public double getGrade() {
  return grade;
 }
 public void setGrade(double grade) {
  this.grade = grade;
 }
 @Override
 public String toString() {
  return "学生姓名:"+name+", 学生住址:"+location+", 准考证号:"+examid+", 身份证号:"+idcard+", 成绩:"+grade;
 }
 }

 

俗称javabean的StudentDao类

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

 

public class StudentDao {
 
 private static XmlUtils utils = new XmlUtils();
 public static void add(Student student){
  try {
   
   Document document = utils.getDocument();  //获得要操作的xml文档的document对象
   
   //创建各节点
   Element student_node = document.createElement("student");
   Element name_node = document.createElement("name");
   Element location_node = document.createElement("location");
   Element grade_node = document.createElement("grade");
   
   //给各节点添加属性和值
   student_node.setAttribute("examid", student.getExamid());
   student_node.setAttribute("idcard", student.getIdcard());
   name_node.setTextContent(student.getName());
   location_node.setTextContent(student.getLocation());
   grade_node.setTextContent(student.getGrade()+"");  //
   
   //将各节点添加到相应位置
   student_node.appendChild(name_node);
   student_node.appendChild(location_node);
   student_node.appendChild(grade_node);
   //student_node.getParentNode().appendChild(student_node);
   document.getElementsByTagName("exam").item(0).appendChild(student_node);
   
   //将内存中的document对象重新写入xml文档
   utils.write2Xml(document);
   //System.out.println("添加学生信息成功");
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }
 
 public static boolean delete(String name){
  
  try {
   Document document = utils.getDocument(); //获得要操作的xml文档的document对象
   NodeList list = document.getElementsByTagName("name");
   for(int i=0; list!=null && i<list.getLength(); i++){
    Node node = list.item(i);
    if(node.getTextContent().equals(name)){
     node.getParentNode().getParentNode().removeChild(node.getParentNode());
     utils.write2Xml(document);
     return true;
    }
   }
   return false;
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }
 
 public static Student find(String examid){
  try {
   Student s = new Student();
   Document document = utils.getDocument();
   NodeList list = document.getElementsByTagName("student");
   for(int i=0; list!=null && i<list.getLength(); i++){
    Element node = (Element) list.item(i);
    if(node.getAttribute("examid").equals(examid)){
     s.setExamid(examid);
     s.setIdcard(node.getAttribute("idcard"));
     s.setGrade(Double.parseDouble(node.getElementsByTagName("grade").item(0).getTextContent()));
     s.setLocation(node.getElementsByTagName("location").item(0).getTextContent());
     s.setName(node.getElementsByTagName("name").item(0).getTextContent());
     return s;
    }
   }
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
  return null;
 }
}

 

 

封装主函数的Main类

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

 

public class Main {
 private static StudentDao dao = new StudentDao();
 public static void main(String[] args) {
  System.out.println("操作类型: 添加学生信息:(a) 删除学生信息:(b) 查询学生信息:(c)");
  System.out.print("请选择操作类型: ");
  Operation();
 }

 private static void Operation() {
  try {
   Student s = new Student();
   //创建读取键盘的BufferedReader对象,并用转换流InputStreamReader将字节流System.in转换为字符流
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   String type = br.readLine();
   if("a".equals(type)){
    System.out.print("请输入学生姓名:");
    String name = br.readLine();
    System.out.print("请输入学生准考证号:");
    String examid = br.readLine();
    System.out.print("请输入学生身份证号:");
    String idcard = br.readLine();
    System.out.print("请输入学生住址:");
    String location = br.readLine();
    System.out.print("请输入学生成绩:");
    String grade = br.readLine();
    
    s.setExamid(examid);
    s.setGrade(Double.parseDouble(grade));
    s.setIdcard(idcard);
    s.setLocation(location);
    s.setName(name);
    
    dao.add(s);
    System.out.println("添加学生信息成功!!!");
   }else if("b".equals(type)){
    System.out.print("请输入学生姓名: ");
    String name = br.readLine();
    boolean b = dao.delete(name);
    if(b){
     System.out.println("删除学生信息成功!!");
    }else{
     System.out.println("删除学生信息失败,请检查学生姓名是否输入正确!!");
    }
   }else if("c".equals(type)){
    System.out.print("请输入学生准考证号: ");
    String examid = br.readLine();
    s = dao.find(examid);
    if(s!=null){
     System.out.println(s);
    }else{
     System.out.println("此学生信息不存在,请检查准考证号是否输入正确!!");
    }
   }else{
    System.out.println("输入错误!!!!");
    System.out.print("请重新选择操作类型: ");
    Operation();
   }
  } catch (Exception e) {
   e.printStackTrace();

    System.out.println("程序出现未知错误,请致电:1384389438!!!!");
  }
 }

}