首页 技术 正文
技术 2022年11月10日
0 收藏 864 点赞 3,459 浏览 6296 个字
 package itacst.dao; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import itacst.domain.Student;
import itacst.exception.StudentNotExistException;
import itacst.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());
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) {
throw new RuntimeException(e);
//unchecked excpeiton(运行时异常)
}//checked exception(编译时的异常 )
} 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相匹配的学生,new出一个student对象封装这个学生的信息返回
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("grade").item(0).getTextContent())); return s;
}
} return null; } catch (Exception e) {
throw new RuntimeException(e);
} } public void delete(String name) throws StudentNotExistException{ 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 StudentNotExistException(name+"not exist");
}catch(StudentNotExistException e){
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
} }
}
 package itacst.utils; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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/exam.xml"; //获取xml
public static Document getDocument() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(filename);
} //thinking in java
public static void write2Xml(Document document) throws Exception{ TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename)));
} }
 package itacst.test; import org.junit.Test; import itacst.dao.StudentDao;
import itacst.domain.Student;
import itacst.exception.StudentNotExistException; public class StudentDaoTest { @Test
public void testAdd(){
StudentDao dao = new StudentDao();
Student s = new Student();
s.setExamid("121");
s.setName("zero");
s.setIdcard("121");
s.setLocation("guangzhou");
s.setGrade(89);
dao.add(s);
} @Test
public void testFind(){
StudentDao dao = new StudentDao();
Student s = dao.find("121"); System.out.println(s.getName()+s.getIdcard()+s.getExamid()+s.getLocation()+s.getGrade());
} @Test
public void testDelete() throws Exception{
StudentDao dao = new StudentDao();
dao.delete("zero");
} }
 package itacst.domain; public class Student {     private String idcard;
private String examid;
private String name;
private String location;
private double grade; 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 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;
}
}
 package itacst.UI; import itacst.dao.StudentDao;
import itacst.domain.Student;
import itacst.exception.StudentNotExistException; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class Main { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
while(true){
try {
System.out.println("添加学生(a) 删除学生(b) 查询学生(c)");
System.out.println("请输入操作类型:"); 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.setName(name);
s.setExamid(examid);
s.setIdcard(idcard);
s.setLocation(location);
s.setGrade(Double.parseDouble(grade)); StudentDao dao = new StudentDao();
dao.add(s); System.out.println("添加成功"); } else if ("b".equals(type)) { System.out.print("请输入要删除的学生姓名:");
String name = br.readLine(); StudentDao dao = new StudentDao();
try {
dao.delete(name);
System.out.println("删除成功!");
} catch (StudentNotExistException e) { e.printStackTrace();
System.out.println("your delete user not exist!!");
} } else if ("c".equals(type)) { } else {
System.out.println("unsupport your choice!!");
} } catch (IOException e) { System.out.println("sorry error....");
} }
} }
 <?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
<student examid="222" idcard="111">
<name>zhangsan</name>
<location>shenyang</location>
<grade>89</grade>
</student>
<student examid="444" idcard="333">
<name>lisi</name>
<location>dalian</location>
<grade>97</grade>
</student> <student examid="321" idcard="321"><name>zero</name><location>guangzhou</location><grade>87.9</grade></student></exam>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905