使用Xml文档进行数据的保存的学生成绩管理系统

有三种操作:

1.添加学生信息

2.删除学生信息

3.查看学生成绩



代码:

package cn.itcast.dao;

import java.io.FileOutputStream;

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

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

import cn.itcast.bean.Student;

public class StudentDao {

	public static void add(Student stu) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("src/Exam.xml");
		
		Element e1 = (Element) document.getElementsByTagName("human").item(0);
		
		Element e2 = (Element) document.createElement("student");
		e1.appendChild(e2);
		
		Element e21 = (Element) document.createElement("usrname");
		e21.setTextContent(stu.getUsrname());
		
		Element e22 = (Element) document.createElement("ticketNum");
		e22.setTextContent(stu.getTicketNum());
		
		Element e23 = (Element) document.createElement("idNum");
		e23.setTextContent(stu.getIdNum());
		
		Element e24 = (Element) document.createElement("location");
		e24.setTextContent(stu.getLocation());
		
		Element e25 = (Element) document.createElement("gradeNum");
		e25.setTextContent(stu.getGradeNum());
		
		e2.appendChild(e21);
		e2.appendChild(e22);
		e2.appendChild(e23);
		e2.appendChild(e24);
		e2.appendChild(e25);
		
		TransformerFactory ttfactory = TransformerFactory.newInstance();
		Transformer tf = ttfactory.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/Exam.xml")));
		
	}

	public static void delete(String usrname) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("src/Exam.xml");
		
		NodeList list = document.getElementsByTagName("usrname");
		for(int i=0;i<list.getLength();i++){
			String content = list.item(i).getTextContent();
			if(content.equals(usrname)){
				list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode());
			}
		}
		
		TransformerFactory ttfactory = TransformerFactory.newInstance();
		Transformer tf = ttfactory.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/Exam.xml")));
	}

	public static void read(String ticketNum) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("src/Exam.xml");
		
		NodeList list = document.getElementsByTagName("ticketNum");
		for(int i=0;i<list.getLength();i++){
			String num = list.item(i).getTextContent();
			if(num.equals(ticketNum)){
				Node root = list.item(i).getParentNode();
				list(root);
			}
		}
	}

	private static void list(Node root) {
		NodeList child = root.getChildNodes();
		for(int i=0;i<child.getLength();i++){
			Node node = child.item(i);
			System.out.println(node.getNodeName()+":"+node.getTextContent());
		}
	}

}

完整源码: http://download.csdn.net/detail/tan313/7905557

你可能感兴趣的:(使用Xml文档进行数据的保存的学生成绩管理系统)