exam.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<exam>
<student cid="440440199002152413" exid="1359070701">
<name>ZhangSan</name>
<gread>83</gread>
<location>GuangDong.ZhuHai</location>
</student>
<student cid="440102198902152413" exid="1359070702">
<name>LiSi</name>
<gread>79</gread>
<location>GuangDong.GuangZhou</location>
</student>
</exam>
—————————————————————————————————————————————————————————————————————————————
package cn.zen.main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.junit.Test;
import cn.zen.dao.StudentDao;
import cn.zen.entity.Student;
import cn.zen.exception.StudentNotExistsException;
public class StudentDaoTest {
/**
* @param args
*/
public static void main(String[] args) {
StudentDao stuDao = new StudentDao();
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out
.println("(A) query student (B)add student (C)delete student");
System.out.print("please input type:");
try {
String type = br.readLine().toString();
if (type.equalsIgnoreCase("A")) {
System.out.print("pealse input student name:");
Student stu = stuDao.findStubyName(br.readLine().toString());
System.out.println("----------------------------------");
System.out.println("cid:" + stu.get_cid());
System.out.println("exid:" + stu.get_exid());
System.out.println("name:" + stu.get_name());
System.out.println("gread:" + stu.get_gread());
System.out.println("location:" + stu.get_location());
}
if (type.equalsIgnoreCase("B")) {
Student stu = new Student();
System.out.print("pealse input student cid:");
stu.set_cid(br.readLine().toString());
System.out.print("pealse input student exid:");
stu.set_exid(br.readLine().toString());
System.out.print("pealse input student name:");
stu.set_name(br.readLine().toString());
System.out.print("pealse input student gread:");
stu.set_gread(Double.parseDouble(br.readLine()));
System.out.print("pealse input student location:");
stu.set_location(br.readLine().toString());
boolean isOk = stuDao.addStudent(stu);
if (isOk) {
System.out.println("add success!");
} else {
System.out.println("add failed!");
}
}
if (type.equalsIgnoreCase("C")) {
System.out.print("pealse input student name:");
boolean isOk = stuDao.deleteStuByName1(br.readLine().toString());
if (isOk) {
System.out.println("delete success!");
} else {
System.out.println("delete failed!");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("input type is error!");
}
}
@Test
public void addStudent() {
StudentDao stuDao = new StudentDao();
Student stu = new Student();
stu.set_cid("440102198902132013");
stu.set_exid("1359070703");
stu.set_name("WangWu");
stu.set_gread(91);
stu.set_location("GuangDong.GuangZhou");
stuDao.addStudent(stu);
}
@Test
public void deleteStuByName() {
try {
StudentDao stuDao = new StudentDao();
stuDao.deleteStuByName("WangWu");
} catch (StudentNotExistsException e) {
// TODO Auto-generated catch block
System.out.println("this student is not exitst!");
}
}
@Test
public void deleteStuByName1() {
StudentDao stuDao = new StudentDao();
boolean isOk = stuDao.deleteStuByName1("WangWu");
System.out.println(isOk);
}
@Test
public void findStuByName() {
StudentDao stuDao = new StudentDao();
stuDao.findStubyName("WangWu");
}
}
—————————————————————————————————————————————————————————————————————————————
package cn.zen.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import cn.zen.entity.Student;
import cn.zen.exception.StudentNotExistsException;
import cn.zen.util.DocumentUtil;
public class StudentDao {
public Student findStubyName(String _name) {
try {
Student stu = null;
Document doc = DocumentUtil.fetchDocument();
NodeList nameNodeList = doc.getElementsByTagName("name");
for (int i = 0; i < nameNodeList.getLength(); i++) {
Node nameNode = nameNodeList.item(i);
if (nameNode.getTextContent().equals(_name)) {
stu = new Student();
// get name node parent
Element stuNode = (Element) nameNode.getParentNode();
stu.set_cid(stuNode.getAttribute("cid"));
stu.set_exid(stuNode.getAttribute("exid"));
stu.set_name(stuNode.getElementsByTagName("name").item(0)
.getTextContent());
stu.set_gread(Double.parseDouble(stuNode
.getElementsByTagName("gread").item(0).getTextContent()));
stu.set_location(stuNode.getElementsByTagName("location").item(0)
.getTextContent());
}
}
return stu;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public boolean addStudent(Student stu) {
try {
boolean isOk = false;
Document doc = DocumentUtil.fetchDocument();
Element eStu = doc.createElement("student");
eStu.setAttribute("cid", stu.get_cid());
eStu.setAttribute("exid", stu.get_exid());
Element eName = doc.createElement("name");
eName.setTextContent(stu.get_name());
Element eGread = doc.createElement("gread");
eGread.setTextContent(stu.get_gread() + "");
Element eLocation = doc.createElement("location");
eLocation.setTextContent(stu.get_location());
eStu.appendChild(eName);
eStu.appendChild(eGread);
eStu.appendChild(eLocation);
Node examNode = doc.getElementsByTagName("exam").item(0);
examNode.appendChild(eStu);
if (DocumentUtil.transformerCommit(doc)) {
isOk = true;
}
return isOk;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void deleteStuByName(String _name) throws StudentNotExistsException {
try {
Document doc = DocumentUtil.fetchDocument();
NodeList nameNodeList = doc.getElementsByTagName("name");
for (int i = 0; i < nameNodeList.getLength(); i++) {
Node nameNode = nameNodeList.item(i);
if (nameNode.getTextContent().equals(_name)) {
nameNode.getParentNode().getParentNode()
.removeChild(nameNode.getParentNode());
DocumentUtil.transformerCommit(doc);
return;
}
}
throw new StudentNotExistsException();
} catch (StudentNotExistsException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public boolean deleteStuByName1(String _name) {
try {
Document doc = DocumentUtil.fetchDocument();
NodeList nameNodeList = doc.getElementsByTagName("name");
boolean isOk = false;
for (int i = 0; i < nameNodeList.getLength(); i++) {
Node nameNode = nameNodeList.item(i);
if (nameNode.getTextContent().equals(_name)) {
nameNode.getParentNode().getParentNode()
.removeChild(nameNode.getParentNode());
if (DocumentUtil.transformerCommit(doc)) {
isOk = true;
}
}
}
return isOk;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
—————————————————————————————————————————————————————————————————————————————
package cn.zen.util;
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;
public class DocumentUtil {
private static String fileName = "src/exam.xml";
public static Document fetchDocument() throws Exception {
DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = bf.newDocumentBuilder();
return docBuilder.parse(fileName);
}
public static boolean transformerCommit(Document doc) throws Exception {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(
new FileOutputStream(fileName)));
return true;
}
}
—————————————————————————————————————————————————————————————————————————————
package cn.zen.entity;
public class Student {
private String _cid;
private String _exid;
private String _name;
private double _gread;
public String get_cid() {
return _cid;
}
public void set_cid(String _cid) {
this._cid = _cid;
}
public String get_exid() {
return _exid;
}
public void set_exid(String _exid) {
this._exid = _exid;
}
public String get_name() {
return _name;
}
public void set_name(String _name) {
this._name = _name;
}
public double get_gread() {
return _gread;
}
public void set_gread(double _gread) {
this._gread = _gread;
}
public String get_location() {
return _location;
}
public void set_location(String _location) {
this._location = _location;
}
private String _location;
}
—————————————————————————————————————————————————————————————————————————————
package cn.zen.exception;
public class StudentNotExistsException extends Exception {
public StudentNotExistsException() {
// TODO Auto-generated constructor stub
}
public StudentNotExistsException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public StudentNotExistsException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public StudentNotExistsException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
}