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、实现学生的删除功能
模块:
强调几点:
- 先写Javabean,做开发是从底层开发到上层。Javabean ---> dao ---> 界面。
- 不同模块的程序放在不同的包里面,这样看到包的名字就知道是什么模块。
- 数据实体,放到一个叫 domain 的包里面。封装 Student 对象。
- 数据库里面存放什么数据,就在 Student 对象里封装什么属性。再生成 get、set 方法。
- 再写dao,你这个软件给用户提供些什么方法,就在dao里实现什么方法。void add(Student s)、void delete(String name)、Student find(String examid)。
- 根据用户的需求来设置参数,用户希望通过什么来查找,你就设置什么参数。
- 这三个方法都有共同的代码,都要将XML文档封装成 Document 对象,都要将数据存储到XML文档中去。抽取出来封装到工具类里。放在工具包里。
- 建 utils 包来放工具类。放 XmlUtils 类。工具类的所有方法都是静态的。public static Document getDocument()、public static void write2Xml(Document document)。
- to 要简写成 2 (到,至 的意思)、for 要简写成 4 (给 的意思)。
- 界面层打包到 view 包里。Main 是有 main 函数的可运行程序。new BufferedReader(new InputStreamReader(System.in)); String type = br.readLine();
String类
boolean | equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写。 |
!注意:
比较两个字符串一定要用 equals 方法!
Double类
static double | parseDouble(String s) 返回一个新的 double 值,该值被初始化为用指定 String 表示的值,这与 Double 类的 valueOf 方法一样。 |
代码:
student.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="111" idcard="123">
<name>黎明</name>
<location>上海</location>
<grade>98.5</grade>
</student>
</exam>
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;
}
}
package cn.itcast.utils;
import java.io.File;
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;
public class XmlUtils {
public static Document getDocument() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/student.xml"));
return document;
}
public static void write2Xml(Document document) throws Exception{
TransformerFactory fac = TransformerFactory.newInstance();
Transformer tf = fac.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new File("src/student.xml")));
}
}
package cn.itcast.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import cn.itcast.domain.Student;
import cn.itcast.utils.XmlUtils;
public class StudentDao {
public void add(Student student){
try {
Document document = XmlUtils.getDocument();
Element student_node = document.createElement("student");
student_node.setAttribute("idcard", student.getIdcard());
student_node.setAttribute("examid", student.getExamid());
Element name = document.createElement("name");
name.setTextContent(student.getName());
Element location = document.createElement("location");
location.setTextContent(student.getLocation());
Element grade = document.createElement("grade");
grade.setTextContent(student.getGrade()+"");
student_node.appendChild(name);
student_node.appendChild(location);
student_node.appendChild(grade);
Node exam = document.getElementsByTagName("exam").item(0);
exam.appendChild(student_node);
XmlUtils.write2Xml(document);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void delete(String name){
try {
Document document = XmlUtils.getDocument();
NodeList nameList = document.getElementsByTagName("name");
for(int x=0; x<nameList.getLength(); x++){
Node nameNode = nameList.item(x);
if(nameNode.getTextContent().equals(name)){
Node student = nameNode.getParentNode();
student.getParentNode().removeChild(student);
XmlUtils.write2Xml(document);
return;
}
}
throw new RuntimeException("对不起,您要删除的学生不存在!"); //异常也是一种返回值
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Student find(String examid){
try {
Document document = XmlUtils.getDocument();
NodeList studentList = document.getElementsByTagName("student");
for(int x=0; x<studentList.getLength(); x++){
Element studentEle = (Element)studentList.item(x);
if(studentEle.getAttribute("examid").equals(examid)){
String idcard = studentEle.getAttribute("idcard");
String name = studentEle.getElementsByTagName("name").item(0).getTextContent();
String location = studentEle.getElementsByTagName("location").item(0).getTextContent();
String grade = studentEle.getElementsByTagName("grade").item(0).getTextContent();
Student student = new Student();
student.setExamid(examid);
student.setIdcard(idcard);
student.setName(name);
student.setLocation(location);
student.setGrade(Double.parseDouble(grade));
return student;
}
}
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package cn.itcast.view;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import cn.itcast.dao.StudentDao;
import cn.itcast.domain.Student;
public class Main {
public static void main(String[] args) {
System.out.println("添加用户:(a) 删除用户:(b) 查询成绩:(c)");
System.out.print("请输入操作类型:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String type = br.readLine();
if(type.equalsIgnoreCase("a")){
try {
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("请输入学生成绩:");
Double grade = Double.parseDouble(br.readLine());
Student student = new Student();
student.setName(name);
student.setExamid(examid);
student.setIdcard(idcard);
student.setLocation(location);
student.setGrade(grade);
StudentDao sd = new StudentDao();
sd.add(student);
System.out.println("恭喜您,添加成功!");
} catch (Exception e) {
System.out.println("对不起,添加失败!");
}
}else if(type.equalsIgnoreCase("b")){
try {
System.out.print("请输入学生姓名:");
String name = br.readLine();
StudentDao sd = new StudentDao();
sd.delete(name);
System.out.println("成功删除!");
} catch (Exception e) {
System.out.println("对不起,您要删除的学生不存在!");
}
}else if(type.equalsIgnoreCase("c")){
try {
System.out.print("请输入学生准考证号:");
String examid = br.readLine();
StudentDao sd = new StudentDao();
Student student = sd.find(examid);
if(student==null){
System.out.println("您查询的学生不存在!");
}else{
System.out.println("您查询的学生信息为:");
System.out.println("姓名:"+student.getName()+
" 身份证号:"+student.getIdcard()+
" 所在地:"+student.getLocation()+
" 成绩:"+student.getGrade());
}
} catch (Exception e) {
System.out.println("对不起,查找失败!");
}
}else{
System.out.println("不支持此操作!");
}
} catch (IOException e) {
System.out.println("对不起,操作失败!");
}
}
}