加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML练习案例(学生管理系统)

发布时间:2020-12-16 05:23:12 所属栏目:百科 来源:网络整理
导读:1 、以如下格式的 exam.xml 文件为例 ? xml version = "1.0" encoding = "UTF-8" standalone = "no" ? exam student idcar d = "111" examid = "222" name 张三 / location 沈阳 grade 89 student "333" "444" 李四 大连 97 2、编程实现如下功能 3、实现学

1、以如下格式的exam.xml文件为例

<?xmlversion="1.0"encoding="UTF-8"standalone="no"?>

<exam>

studentidcard="111"examid="222">

name>张三</location>沈阳grade>89student>

"333""444">李四>大连>97>

>


2、编程实现如下功能


3、实现学生信息的添加


4、实现学生信息的查询


5、实现学生的删除功能


XML当做数据库,完成系统的增加、删除、查找功能!

之所以能把XML当做是数据库,是因为XML可以体现数据之间的关系,可以完成整个数据库单独模块内容的操作!利用三层架构,将程序的功能拆分,某部分代码负责某部分的功能实现!


将整个程序进行拆分:可以分为:Utils工具类UI界面设计DAO具体操作:增删改查数据库XML文档!(可以试试txt文本当做数据库,要想办法构建数据之间的联系,方便于整体操作!)备注:在实际的代码实现阶段,还会需要一个测试类,方便于在整个程序还没有完成的时候对程序的部分功能进行测试,做一步,测试一步,以免整个程序完成后由于页面太多或者是代码量太大给查找错误造成更大的负担!所以这里会多一个测试类!


XmlUtils类:工具类:得到Document对象和更新文档这些工具性质的操作!

packagecom.itheima.utils;

importjavax.xml.parsers.DocumentBuilder;

importjavax.xml.parsers.DocumentBuilderFactory;

importjavax.xml.transform.Transformer;

importjavax.xml.transform.TransformerFactory;

importjavax.xml.transform.dom.DOMSource;

importjavax.xml.transform.stream.StreamResult;

importorg.w3c.dom.Document;

/**

*工具类:

*得到document对象

*更新XML文档

*/

publicclassXmlUtils{

staticDocumentgetDocument()throwsException{

//获得Document对象

DocumentBuilderdb=DocumentBuilderFactory.newInstance().newDocumentBuilder();

Documentdocument=db.parse("src/exam.xml");

returndocument;

}

//更新XML文档

staticvoidwrite2xml(Documentdocument)throwsException{

Transformerts=TransformerFactory.newInstance().newTransformer();

ts.transform(newDOMSource(document),newStreamResult("src/exam.xml"));

}

}


Student对象类:对象数据的存储或传递

packagecom.itheima.domain;

//代表学生的JavaBean

classStudent{

privateStringidcard;//身份证号

examid;//准考证号

name;

location;

privatefloatgrade;

publicStringgetIdcard(){

returnidcard;

}

voidsetIdcard(Stringidcard){

this.idcard=idcard;

}

publicStringgetExamid(){

examid;

}

voidsetExamid(Stringexamid){

examid=examid;

}

publicStringgetName(){

name;

}

voidsetName(Stringname){

name=name;

}

publicStringgetLocation(){

location;

}

voidsetLocation(Stringlocation){

location=location;

}

floatgetGrade(){

grade;

}

voidsetGrade(floatgrade){

grade=grade;

}

@Override

publicStringtoString(){

return"姓名:"+name+"t身份证号:"+idcard+"t准考证号:"+examid+"t地址:"+location+"t成绩:"+grade;

}

}


StudentDao对象类:最核心的部分,增删改查的功能实现部分!


packagecom.itheima.dao;

importorg.w3c.dom.Document;

importorg.w3c.dom.Element;

importorg.w3c.dom.Node;

importorg.w3c.dom.NodeList;

importcom.itheima.domain.Student;

importcom.itheima.utils.XmlUtils;

//完成XML文档的查询、删除、增加

classStudentDao{

//添加

booleanaddStudent(Studentstudent){

booleanresult=true;

try{

//1、得到document对象

Documentdocument=XmlUtils.getDocument();

//2、创建学生节点,并设置其属性<studentexamid="222"idcard="111">

ElementstudentE=document.createElement("student");

studentE.setAttribute("idcard",student.getIdcard());

studentE.setAttribute("examid",student.getExamid());

//3、创建name节点,并设置其主题内容<name>张三丰</name>

ElementnameE=document.createElement("name");

nameE.setTextContent(student.getName());

//4、创建location节点,并设置其主题内容<location>郑州</location>

ElementloactionE=document.createElement("location");

loactionE.setTextContent(student.getLocation());

//5、创建grade节点,并设置其主题内容<grade>100</grade>

ElementgradeE=document.createElement("grade");

gradeE.setTextContent(student.getGrade()+"");

//6、建立他们之间的关系

/**

*<exam>

<studentexamid="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(Exceptione){

thrownewRuntimeException(e);

}

returnresult;

}

//根据姓名进行删除

booleanremoveStudent(Stringname){

false;

try{

Documentdocument=XmlUtils.getDocument();

//1、得到name节点

NodeListlist=document.getElementsByTagName("name");

//2、循环遍历所有的name节点,比对主体内容与参数是否匹配

for(inti=0;i<list.getLength();i++){

Nodenode=list.item(i);

if(node.getTextContent().equals(name.trim())){

//3、是的话得到爷爷删除爸爸,因为这里是根据姓名进行查找的

node.getParentNode().getParentNode().removeChild(node.getParentNode());

//4、更新XML文档,返回true

XmlUtils.write2xml(document);

result=true;

}

}

}returnresult;

}

//根据准考证号查询

publicStudentqueryStudent(Stringexamid){

Studentstudent=null;

//1、得到name节点

NodeListlist=document.getElementsByTagName("student");

inti=0;i<list.getLength();i++){

Nodenode=list.item(i);

Elemente=(Element)node;

if(e.getAttribute("examid").equals(examid.trim())){//忽略前端和尾部的空白!

//3、匹配的话,创建student对象

student=newStudent();

//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(Exceptione){

newRuntimeException(e);

}

returnstudent;//返回student对象,覆写它的toString方法

}

}

StudentDaoTest:写程序过程中的测试类(此处并没有用到Junit)!

packagecom.itheima.test;

importcom.itheima.dao.StudentDao;

classStudentDaoTest{

voidmain(String[]args){

/*Students=newStudent();

s.setExamid("123");

s.setGrade(100f);

s.setIdcard("124");

s.setLocation("北京");

s.setName("章泽天");*/

StudentDaosd=newStudentDao();

//sd.addStudent(s);

//Students=sd.queryStudent("444");

//System.out.println(s);

//sd.removeStudent("章泽天");

System.out.println(sd.queryStudent("222"));

}

}

Main:主页UI类(虽然这里界面仍然是控制台)


packagecom.itheima.ui;

importjava.io.BufferedReader;

importjava.io.InputStreamReader;

importcom.itheima.dao.StudentDao;

importcom.itheima.domain.Student;

classMain{

//1、打印操作提示

System.out.println("a、添加学生tb、删除学生tc、查询学生");

System."请输入操作类型:");

//2、接收用户的输入

try{

StudentDaodao=newStudentDao();

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

Stringoperation=br.readLine();

//3、对用户输入的内容进行判断

if("a".equals(operation)){

//添加操作

System."请输入学生姓名:");

Stringname=br.readLine();

System."请输入学生准考证号:");

Stringexamid=br.readLine();

System."请输入学生身份证号:");

Stringidcard=br.readLine();

System."请输入学生地址:");

Stringlocation=br.readLine();

System."请输入学生成绩:");

Stringgrade=br.readLine();

Studentstudent=newStudent();

student.setExamid(examid);

student.setGrade(Float.parseFloat(grade));

student.setIdcard(idcard);

student.setLocation(location);

student.setName(name);

booleanresult=dao.addStudent(student);

if(result){

System."---添加成功---");

}else{

System."---添加失败---");

}

}else"b".equals(operation)){

//删除

System."请输入要删除的学生姓名:");

Stringname=br.readLine();

booleanresult=dao.removeStudent(name);

"---删除成功---");

}"---删除失败或者学生不存在---");

}

}"c".equals(operation)){

//查询

System."请输入准考证号:");

Stringexamid=br.readLine();

Students=dao.queryStudent(examid);

if(s==null){

System."学生不存在");

}out.println(s);

}

}else{

System."你火星来的");

}

}catch(Exceptione){

e.printStackTrace();

System."程序忙,请重新启动");

}

}

}


这个程序最重要的还是XML节点操作的应用,顺带的加上了简单的三层构架!工具类,业务实现(实际操作)类,UI类!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读