XML案例(简单的考生成绩管理系统)

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

<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
<student examid="222" idcard="111">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>

<student examid="444" idcard="333">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
</exam>

2.编程实现如下功能

添加用户:(a) 删除用户:(b) 查询成绩:(c)

请输入操作类型:

3.实现学生信息的添加

添加用户:(a) 删除用户:(b) 查询成绩:(c)

请输入操作类型:a

请输入学生姓名:张三

请输入学生准考证号:22202123

请输入学生身份证号:233434343344343

请输入学生所在地:沈阳

请输入学生成绩:89

-------添加数据成功--------

4.实现学生信息查询

添加用户:(a) 删除用户:(b) 查询成绩:(c)

请输入操作类型:c

请输入查询的学生准考证号:22202123

您查询的学生信息为:

姓名:张三,身份证号:233434343344343,准考证号:22202123,地区:沈阳,成绩:89

5.实现学生的删除功能

添加用户:(a) 删除用户:(b) 查询成绩:(c)

请输入操作类型:b

请输入删除的学生姓名:张三

-------已成功删除学生信息--------

6.代码

1)exam.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
<student examid="222" idcard="111">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>

<student examid="444" idcard="333">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
</exam>

2)cn.itcast.dao包,获取数据

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.StudentNotExistException;
import cn.itcast.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 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++) {
Element student_name= (Element) list.item(i);
if (student_name.getTextContent().equals(name)) {
student_name.getParentNode().getParentNode().removeChild(student_name.getParentNode());
XmlUtils.write2Xml(document);
return;
}
}
throw new StudentNotExistException(name+"不存在!!");
}catch(StudentNotExistException se)
{
throw se;
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}

3)cn.itcast.domain包,封装数据

package cn.itcast.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;
}

}

4) cn.itcast.exception包,异常信息

package cn.itcast.exception;

public class StudentNotExistException extends Exception {

public StudentNotExistException() {
// TODO Auto-generated constructor stub
}

public StudentNotExistException(String message) {
super(message);
// TODO Auto-generated constructor stub
}

public StudentNotExistException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}

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

}

5) cn.itcast.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.StudentNotExistException;

public class Main {

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();

StudentDao dao = new StudentDao();
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);

dao.add(s);
System.out.println("添加成功!!!");
} else if ("b".equals(type)) {
System.out.print("请输入要删除的学生姓名:");
String name = br.readLine();
try {
dao.delete(name);
System.out.println("删除成功!!!");
}
catch(StudentNotExistException e){
System.out.println("您要删除的用户不存在!!!");
}
} else if ("c".equals(type)) {

} else {
System.out.println("不支持您的操作!");
}

} catch (Exception e) {
e.printStackTrace();
System.out.println("对不起,俺出错了!!");
}
}

}

6)cn.itcast.utils包,工具包,提供一些公用的方法

package cn.itcast.utils;

import java.io.FileOutputStream;
import java.io.IOException;

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;
import org.xml.sax.SAXException;
//工具类,方法通常设置为静态的
public class XmlUtils {

private static String filename="src/exam.xml";

public static Document getDocument() throws Exception{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
return builder.parse(filename);
}
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) ));
}
}

7)junit.test包,测试所写的代码

package junit.test;

import org.junit.Test;

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

public class StudentDaoTest {

public StudentDaoTest() {
// TODO Auto-generated constructor stub
}

@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();
Student s=dao.find("121");
System.out.println("getExamid:"+s.getExamid());
System.out.println("getName :"+s.getName());
System.out.println("getLocation:"+s.getLocation());
System.out.println("getGrade:"+s.getGrade());
System.out.println("getIdcard:"+s.getIdcard());
}

@Test
public void testdelete() throws StudentNotExistException {
StudentDao dao = new StudentDao();
dao.delete("aa");

}

}

原文地址:https://www.cnblogs.com/xiaohuihui123/p/4359192.html