cn.dimain bean存在的包Student.java
cn.dao 对xml的增删改查 StudentDao.java
cn.exception 存放异常 StudentNotExistException.java
cn.utils 存放工具类 XmlUtils.java
juliet.test 存放测试类 StudentTest.java
/java/src/cn/UI/Main.java 存放主程序
xml文件:src目录下
/java/src/book.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<exam>
<student examid="222" idcard="111">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>
<student examid="444" idcard="333">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
<student examid="127" idcard="121">
<name>aa</name>
<location>beijing</location>
<grade>89.0</grade>
</student>
<student examid="127" idcard="121">
<name>aa</name>
<location>beijing</location>
<grade>89.0</grade>
</student>
<student examid="c" idcard="d">
<name>b</name>
<location>e</location>
<grade>12.0</grade>
</student>
</exam>
/java/src/cn/domin/Student.java
package cn.domin;
public class Student {
private String idcard;
private String examid;
private String location;
private Double grade;
private String name;
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getExamid() {
return examid;
}
public void setExamid(String examid) {
this.examid = examid;
}
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/java/src/cn/utils/XmlUtils.java
package cn.utils;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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;
public class XmlUtils {
private static String filename="src/example.xml";
public static Document getDocument() throws Exception{
//得到
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(filename);
return document;
}
public static void write2xml(Document document) throws FileNotFoundException, TransformerException{
TransformerFactory tsf = TransformerFactory.newInstance();
Transformer transform=tsf.newTransformer();
transform.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename)));
}
}
/java/src/cn/dao/StudentDao.java
package cn.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import cn.domin.Student;
import cn.exception.StudentNotExitException;
import cn.utils.XmlUtils;
public class StudentDao {
public void add(Student s){
try {
Document document = XmlUtils.getDocument();
//创建封装学生的标签
Element student_tag=document.createElement("student");
student_tag.setAttribute("idcard", s.getIdcard());
student_tag.setAttribute("examid", s.getExamid());
//创建用于风窗学生姓名、所在地和成绩的标签
Element name=document.createElement("name");
Element location=document.createElement("location");
Element grade=document.createElement("grade");
name.setTextContent(s.getName());
location.setTextContent(s.getLocation());
//double + ""得到新的字符串
grade.setTextContent(s.getGrade()+"");
student_tag.appendChild(name);
student_tag.appendChild(location);
student_tag.appendChild(grade);
//将封装了的学生标签挂载文档上
document.getElementsByTagName("exam").item(0).appendChild(student_tag);
//更新内存
XmlUtils.write2xml(document);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e); //转换成运行时异常 给上一层处理
} //checked 异常 编译时异常
}
public Student find(String examid){
try {
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("student");
for (int i=0;i<list.getLength();i++){
Element student_tag=(Element) list.item(i);
if (student_tag.getAttribute("examid").equals(examid)){
//找到与examID相匹配的学生 创建学生对象并且返回
Student s = new Student();
s.setExamid(examid);
s.setIdcard(student_tag.getAttribute("idcard"));
s.setName(student_tag.getElementsByTagName("name").item(0).getTextContent());
s.setLocation(student_tag.getElementsByTagName("location").item(0).getTextContent());
s.setGrade(Double.parseDouble(student_tag.getElementsByTagName("location").item(0).getTextContent()));
return s;
}
}
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void delete(String name) throws StudentNotExitException{
try {
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("name");
for (int i=0;i<list.getLength();i++){
if (list.item(i).getTextContent().equals(name)){
list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode());
XmlUtils.write2xml(document);
return;
}
}
throw new StudentNotExitException(name + "不存在");
} catch (StudentNotExitException e) {
throw e;
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}
/java/src/cn/exception/StudentNotExitException.java
package cn.exception;
public class StudentNotExitException extends Exception {
public StudentNotExitException() {
// TODO Auto-generated constructor stub
}
public StudentNotExitException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public StudentNotExitException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public StudentNotExitException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
}
/java/src/junit/test/StudentTest.java
package junit.test;
import org.junit.Test;
import cn.dao.StudentDao;
import cn.domin.Student;
public class StudentTest {
@Test
public void testadd(){
StudentDao dao = new StudentDao();
Student s = new Student();
s.setExamid("127");
s.setGrade(89.0);
s.setIdcard("121");
s.setLocation("beijing");
s.setName("aa");
dao.add(s);
}
}
/java/src/cn/UI/Main.java
package cn.UI;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import cn.dao.StudentDao;
import cn.domin.Student;
import cn.exception.StudentNotExitException;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
try {
// TODO Auto-generated method stub
System.out.println("添加用戶(a) 刪除用戶(b) 查找用戶(c)");
System.out.print("請輸入炒作類型:");
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();
Student s = new Student();
s.setExamid(examid);
s.setGrade(Double.parseDouble(grade));
s.setIdcard(idcard);
s.setLocation(location);
s.setName(name);
StudentDao dao = new StudentDao();
dao.add(s);
System.out.println("add sucess!");
}else if("b".equals(type)){
System.out.print("请输入需要删除的学生姓名:");
String name = br.readLine();
try {
StudentDao dao = new StudentDao();
dao.delete(name);
System.out.println("删除成功!");
} catch (StudentNotExitException e) {
System.out.println("您要删除的用户不存在!");
}
}else if("c".equals(type)){
}else{
System.out.println("請輸入正確選項");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("sorry one err");
}
}
}