xml案例(考生成绩管理系统)
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>
xml案例(考生成绩管理系统)的更多相关文章
- XML案例(简单的考生成绩管理系统)
1.以如下格式的exam.xml文件为例 <?xml version="1.0" encoding="UTF-8" standalone="no ...
- 第一次写C语言小程序,可以初步理解学生成绩管理系统的概念
1 成绩管理系统概述 1.1 管理信息系统的概念 管理信息系统(Management Information Systems,简称MIS),是一个不断发展的新型学科,MIS的定义随着科技的进步也在 ...
- Java开学测试-学生成绩管理系统
题目: 1.定义 ScoreInformation 类,其中包括七个私有变量(stunumber, name, mathematicsscore, englishiscore,networkscore ...
- java简单学生成绩管理系统
题目要求: 一. 数据结构要求:(5 分) 1.定义 ScoreInformation 类,其中包括七个私有变量(stunumber, name, mathematicsscore, englishi ...
- java学生成绩管理系统
信1805-1 20183590 田庆辉 石家庄铁道大学 2019 年秋季 ...
- SSM整合案例:图书管理系统
目录 SSM整合案例:图书管理系统 1.搭建数据库环境 2.基本环境搭建 2.1.新建一个Maven项目,起名为:ssmbuild,添加web的支持 2.2.导入pom的相关依赖 2.3.Maven静 ...
- 面向对象案例-学生信息管理系统V0.6
更新版本 面向对象案例 - 学生信息管理系统V1.0 项目要求: 实体类: 学生类: id, 姓名,年龄,性别,成绩 需要使用数组保存学生信息 Student[] allStu 需要完成的方法 1. ...
- 学生成绩管理系统(SSM+MySQL+JSP)
开发工具:Eclipse前端技术:基础:html+css+JavaScript框架:JQuery+H-ui后端技术:Spring+SpringMVC+mybatis模板引擎:JSP数据库:mysql ...
- Java+Eclipse+MySQL+Swing实现学生会考成绩管理系统(免费完整项目)
版权声明:原创不易,本文禁止抄袭.转载,侵权必究! 目录 一.需求开发文档 二.数据库设计文档 三.功能模块部分代码及效果展示 四.完整源码下载 五.作者Info 一.需求开发文档 项目完整文件列表: ...
随机推荐
- Android自动化测试 - 自动化测试工具比较
- Ue4如何在C++中获得获得当前角色的指针?
#include "ThirdPersonPluginCharacter.h" #include "Kismet/GameplayStatics.h" //包含 ...
- URAL1996 Cipher Message 3(KMP + FFT)
题目 Source http://acm.timus.ru/problem.aspx?space=1&num=1996 Description Emperor Palpatine has be ...
- 寻找房间中心zz
Finding the Centroid of a Room Boundary It's been a while since my last post and I'm sure most of yo ...
- BZOJ4435 : [Cerc2015]Juice Junctions
最大流=最小割,而因为本题点的度数不超过3,所以最小割不超过3,EK算法的复杂度为$O(n+m)$. 通过分治求出最小割树,设$f[i][j][k]$表示最小割为$i$时,$j$点在第$k$次分治过程 ...
- [linux]crontab 命令执行问题
在服务器上设置了一个R脚本的crontab任务,死活不执行.在网上搜了很久,终于解决了. 这里主要说一下crontab异常时,该如何排查. 假设cron命令为:* * * * Rscript /you ...
- 科技部专家王涌天:移动AR头显将“让人类重新站起来”
目前,业内普遍认为VR和AR技术将是继移动手机之后的下一代计算平台,将给社会的方方面面带来全新的改变.近日,北京理工大学信息与电子学部主任.科技部863信息技术领域专家组成员王涌天教授对头戴式增强现实 ...
- 1204. Maze Traversal
1204. Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...
- SpringMVC视图机制详解[附带源码分析]
目录 前言 重要接口和类介绍 源码分析 编码自定义的ViewResolver 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门bl ...
- Oracle connect by 树查询之二
先用scott用户下的emp表做实验.emp表有个字段,一个是empno(员工编号),另一个是mgr(上级经理编号)下面是表中所有数据 1 select * from emp start with e ...