微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

XML---学生成绩管理系统 之 DOM解析 [方立勋视频讲解]


生成绩管理系统

0、先把思路定下,自下而上构思




0、分析xml 文档



1、daomain/bean/entity

================================

package cn.itcast.domain;

/**
* 2014-12-16 16:10:37
* Student属性 是XML文档的标签
* @author yungcs
*
*/
public class Student {

private String idcard; //属性1
private String examid; //属性2
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;
}



}
==================================


2、XmlUtils工具类
======================================

package cn.itcast.utils;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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;

//工具包挺长是静态方法

/**
*
* 2014-12-16 下午04:18:50
* @author yungcs
* Xml工具类
*/
public class XmlUtils {


private static String filename = "src/exam.xml";
/**
* 将xml转成document
* @return
* @throws Exception
*/
public static Document getDocument() throws Exception{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //接口要不是调用静态方法,要不就newinstance
DocumentBuilder builder = factory.newDocumentBuilder();

return builder.parse(filename);
}

//thinking in java spring
/**
* 将document转成xml
*/
public static void write2Xml(Document document) throws Exception{

TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();

tf.transform(new DOMSource(document),new StreamResult(filename));
}

}


=========================================

3、StudentDao
============================

package cn.itcast.dao;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import cn.itcast.domain.Student;
import cn.itcast.exception.StudentNotExitsException;
import cn.itcast.utils.XmlUtils;


/**
*
* 2014-12-16 下午04:22:31
* Todo 操作 对xml进行增删查
* @author yungcs
*/
public class StudentDao {

/**
* 增加学生 根据学生对象 [Student] 增加
*/
public void add(Student s){

try {
Document document = XmlUtils.getDocument();//将xml转成document

//创建出用于封装学生姓名、所在地和成绩的标签
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);//将document转成xml


} catch (Exception e) {

//System.out.println("=="+e);
//[!!!!!!注意!!!!!!!]
//将运行时异常转成编译时异常,并且在括号内传入异常参数
//这样是为了把异常的种类报告给上一级
throw new RuntimeException(e); //uncheck exception(运行时异常)
}


}

/**
* 查询学生 根据 [examid] 查询
*/
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)){
//
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;
}
}

} catch (Exception e) {
throw new RuntimeException(e); //uncheck exception(运行时异常)
}

return null;
}

/**
* 删除学生 根据姓名 [name] 删
*/
public void delete(String name) throws StudentNotExitsException{
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 StudentNotExitsException(name+"不存在");
}catch (StudentNotExitsException e) {
throw e; //uncheck exception(运行时异常)
}
catch (Exception e) {
throw new RuntimeException(e); //uncheck exception(运行时异常)
}

}
}

======================================


4、自定义异常
===============================

package cn.itcast.exception;

/**
* 2014-12-16 16:11:24
* 异常统一在一个包类内存放
* @author yungcs
*
*/
public class StudentNotExitsException extends Exception {

public StudentNotExitsException() {
// Todo Auto-generated constructor stub
}

public StudentNotExitsException(String message) {
super(message);
// Todo Auto-generated constructor stub
}

public StudentNotExitsException(Throwable cause) {
super(cause);
// Todo Auto-generated constructor stub
}

public StudentNotExitsException(String message,Throwable cause) {
super(message,cause);
// Todo Auto-generated constructor stub
}

}


=========================
记得勾选倒数第二栏

5、测试类

=================================

package junit.test;

import org.junit.Test;

import cn.itcast.dao.StudentDao;
import cn.itcast.domain.Student;
import cn.itcast.exception.StudentNotExitsException;

/**
* 2014-12-16 16:12:42
* StudentDao 的一个测试类
* @author yungcs
*
*/
public class StudentDaoTest {

@Test
public void testAdd(){

StudentDao dao = new StudentDao();
Student s = new Student();
s.setExamid("121");
s.setGrade(89);
s.setIdcard("121");
s.setLocation("北京");
s.setName("aa");
dao.add(s);
}

@Test
public void testFind(){
StudentDao dao = new StudentDao();
dao.find("121");

//System.out.println("--"+dao.find("121").getGrade());
}

@Test
public void testDelete() throws StudentNotExitsException{
StudentDao dao = new StudentDao();
dao.delete("aa");
//dao.delete("eee");
//System.out.println("--"+dao.find("121").getGrade());
}
}
==================================


6、主UI程序

====================================

package cn.itcast.UI;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import cn.itcast.dao.StudentDao;
import cn.itcast.domain.Student;
import cn.itcast.exception.StudentNotExitsException;

/**
* XML---学生成绩管理系统 之 DOM解析
* 2014-12-16 下午04:29:12
* Todo UI界面设计
* @author yungcs
*/
public class Main {


/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {

try {
System.out.println("添加学生(a) 删除学生(b) 查找学生(c)");
System.out.print("请输入操作类型:");

//读取键盘输入的文字
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.setExamid(examid);
s.setGrade(Double.parseDouble(grade));
s.setIdcard(idcard);
s.setLocation(location);
s.setName(name);

StudentDao dao = new StudentDao();
dao.add(s);
System.out.println("添加成功!");

}else if("b".equals(type)){

System.out.print("请输入要删除的学生:");
String name = br.readLine();

try {
StudentDao dao = new StudentDao();
dao.delete(name);
System.out.println("删除成功!!!");
} catch (StudentNotExitsException e) {
System.out.println("您要删除用户不存在!!!");
}


}else if("c".equals(type)){
//自己写了
}else{
System.out.println("不支持您的操作!!");
}

} catch (IOException e) {
e.printstacktrace(); //
System.out.println("对不起,出错了!");
}


}

}

===========================================

原文地址:https://www.jb51.cc/xml/297514.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。