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 一.需求开发文档 项目完整文件列表: ...
随机推荐
- 1140 分珠 dfs
时间限制:500MS 内存限制:65536K提交次数:24 通过次数:18 题型: 编程题 语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不同,珠子之间有一 ...
- 我的c++学习(3)字符的输入输出
#include "stdafx.h" #include<iostream> using namespace std; int main(void) { /* char ...
- 读取接口XML和批量导入数据SqlBulkCopy
首先是C#处理xml文档 string urlStr = string.Format("http://……?timeBeg={0}&timeEnd={1}", timeBe ...
- Java视频
http://wenku.baidu.com/course/list/512?tagID=143
- C#性能测试方法
用 System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); for (int i ...
- js定时相关函数:
定时相关函数: mytime= setTimeout(vCode, iMilliSeconds [, sLanguage]) -- 单次定时执行指定函数 clearTimeout(iTimeoutID ...
- Spring In Action ③
第三章 最小化Spring xml配置 自动装配(autowiring) 自动检测(autodiscovery) 自动装配 byName.byType.constructor autodetec ...
- Leetcode Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- HDU 4750 Count The Pairs(并查集)
题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- 【BZOJ1088】[SCOI2005]扫雷Mine 递推
调LCT奔溃,刷水调节一下. #include <iostream> #include <cstdio> #include <cstring> using name ...