1、以如下格式的exam.xml文件为例
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<exam>
<student idcard="111" examid="222">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>
<student idcard="333" examid="444">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
</exam>
2、编程实现如下功能
3、实现学生信息的添加
4、实现学生信息的查询
5、实现学生的删除功能
把XML当做数据库,完成系统的增加、删除、查找功能!
之所以能把XML当做是数据库,是因为XML可以体现数据之间的关系,可以完成整个数据库单独模块内容的操作!
利用三层架构,将程序的功能拆分,某部分代码负责某部分的功能实现!
将整个程序进行拆分:
可以分为:
Utils工具类
UI 界面设计
DAO 具体操作:增删改查
数据库 XML文档!(可以试试txt文本当做数据库,要想办法构建数据之间的联系,方便于整体操作!)
备注:在实际的代码实现阶段,还会需要一个测试类,方便于在整个程序还没有完成的时候对程序的部分功能进行测试,做一步,测试一步,以免整个程序完成后由于页面太多或者是代码量太大给查找错误造成更大的负担!
所以这里会多一个测试类!
XmlUtils类:工具类:得到Document对象和更新文档这些工具性质的操作!
package com.itheima.utils;
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;
/**
* 工具类:
* 得到document对象
* 更新XML文档
*/
public class XmlUtils {
public static Document getDocument()throws Exception{
//获得Document对象
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = db.parse("src/exam.xml");
return document;
}
//更新XML文档
public static void write2xml(Document document) throws Exception{
Transformer ts = TransformerFactory.newInstance().newTransformer();
ts.transform(new DOMSource(document), new StreamResult("src/exam.xml"));
}
}
Student对象类:对象数据的存储或传递
package com.itheima.domain;
//代表学生的JavaBean
public class Student {
private String idcard;//身份证号
private String examid;//准考证号
private String name;
private String location;
private float 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 float getGrade() {
return grade;
}
public void setGrade(float grade) {
this.grade = grade;
}
@Override
public String toString(){
return "姓名:"+name+"\t身份证号:"+idcard+"\t准考证号:"+examid+"\t地址:"+location+"\t成绩:"+grade;
}
}
StudentDao对象类:最核心的部分,增删改查的功能实现部分!
package com.itheima.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.itheima.domain.Student;
import com.itheima.utils.XmlUtils;
//完成XML文档的查询、删除、增加
public class StudentDao {
//添加
public boolean addStudent(Student student){
boolean result = true;
try{
//1、得到document对象
Document document = XmlUtils.getDocument();
//2、创建学生节点,并设置其属性<student examid="222" idcard="111">
Element studentE = document.createElement("student");
studentE.setAttribute("idcard", student.getIdcard());
studentE.setAttribute("examid", student.getExamid());
//3、创建name节点,并设置其主题内容<name>张三丰</name>
Element nameE = document.createElement("name");
nameE.setTextContent(student.getName());
//4、创建location节点,并设置其主题内容<location>郑州</location>
Element loactionE = document.createElement("location");
loactionE.setTextContent(student.getLocation());
//5、创建grade节点,并设置其主题内容<grade>100</grade>
Element gradeE = document.createElement("grade");
gradeE.setTextContent(student.getGrade()+"");
//6、建立他们之间的关系
/**
* <exam>
<student examid="222" idcard="111">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>
*/
studentE.appendChild(nameE);
studentE.appendChild(loactionE);
studentE.appendChild(gradeE);
document.getElementsByTagName("exam").item(0).appendChild(studentE);
//7、更新XML文档
XmlUtils.write2xml(document);
result = true;
}catch(Exception e){
throw new RuntimeException(e);
}
return result;
}
//根据姓名进行删除
public boolean removeStudent(String name){
boolean result = false;
try{
Document document = XmlUtils.getDocument();
//1、得到name节点
NodeList list = document.getElementsByTagName("name");
//2、循环遍历所有的name节点,比对主体内容与参数是否匹配
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if(node.getTextContent().equals(name.trim())){
//3、是的话得到爷爷删除爸爸,因为这里是根据姓名进行查找的!
node.getParentNode().getParentNode().removeChild(node.getParentNode());
//4、更新XML文档,返回true
XmlUtils.write2xml(document);
result = true;
}
}
}catch(Exception e){
throw new RuntimeException(e);
}
return result;
}
//根据准考证号查询
public Student queryStudent(String examid){
Student student = null;
try{
Document document = XmlUtils.getDocument();
//1、得到name节点
NodeList list = document.getElementsByTagName("student");
//2、循环遍历所有的name节点,比对主体内容与参数是否匹配
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
Element e = (Element)node;
if(e.getAttribute("examid").equals(examid.trim())){//忽略前端和尾部的空白!
//3、匹配的话,创建student对象
student = new Student();
//4、获取数据并封装到Student对象中
student.setExamid(examid);
student.setIdcard(e.getAttribute("idcard"));//得到节点的属性或者内容,赋给student对象
student.setName(e.getElementsByTagName("name").item(0).getTextContent());
student.setLocation(e.getElementsByTagName("location").item(0).getTextContent());
student.setGrade(Float.parseFloat(e.getElementsByTagName("grade").item(0).getTextContent()));
}
}
}catch(Exception e){
throw new RuntimeException(e);
}
return student;//返回student对象,覆写它的toString方法
}
}
StudentDaoTest:写程序过程中的测试类(此处并没有用到Junit)!
package com.itheima.test;
import com.itheima.dao.StudentDao;
public class StudentDaoTest {
public static void main(String[] args) {
/*Student s = new Student();
s.setExamid("123");
s.setGrade(100f);
s.setIdcard("124");
s.setLocation("北京");
s.setName("章泽天");*/
StudentDao sd = new StudentDao();
//sd.addStudent(s);
//Student s = sd.queryStudent("444");
//System.out.println(s);
//sd.removeStudent("章泽天");
System.out.println(sd.queryStudent("222"));
}
}
Main:主页UI类(虽然这里界面仍然是控制台)
package com.itheima.ui;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.itheima.dao.StudentDao;
import com.itheima.domain.Student;
public class Main {
public static void main(String[] args) {
//1、打印操作提示
System.out.println("a、添加学生\tb、删除学生\tc、查询学生");
System.out.println("请输入操作类型:");
//2、接收用户的输入
try{
StudentDao dao = new StudentDao();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String operation = br.readLine();
//3、对用户输入的内容进行判断
if("a".equals(operation)){
//添加操作
System.out.println("请输入学生姓名:");
String name = br.readLine();
System.out.println("请输入学生准考证号:");
String examid = br.readLine();
System.out.println("请输入学生身份证号:");
String idcard = br.readLine();
System.out.println("请输入学生地址:");
String location = br.readLine();
System.out.println("请输入学生成绩:");
String grade = br.readLine();
Student student = new Student();
student.setExamid(examid);
student.setGrade(Float.parseFloat(grade));
student.setIdcard(idcard);
student.setLocation(location);
student.setName(name);
boolean result = dao.addStudent(student);
if(result){
System.out.println("---添加成功---");
}else{
System.out.println("---添加失败---");
}
}else if("b".equals(operation)){
//删除
System.out.println("请输入要删除的学生姓名:");
String name = br.readLine();
boolean result = dao.removeStudent(name);
if(result){
System.out.println("---删除成功---");
}else{
System.out.println("---删除失败或者学生不存在---");
}
}else if("c".equals(operation)){
//查询
System.out.println("请输入准考证号:");
String examid = br.readLine();
Student s = dao.queryStudent(examid);
if(s==null){
System.out.println("学生不存在");
}else{
System.out.println(s);
}
}else{
System.out.println("你火星来的");
}
}catch(Exception e){
e.printStackTrace();
System.out.println("程序忙,请重新启动");
}
}
}
这个程序最重要的还是XML节点操作的应用,顺带的加上了简单的三层构架!工具类,业务实现(实际操作)类,UI类!