xml jasp dom解析的小案例學生成績系統
阿新 • • 發佈:2018-11-10
cn.dimain bean存在的包Student.java
cn.dao 對xml的增刪改查 StudentDao.java
cn.exception 存放異常 StudentNotExistException.java
cn.utils 存放工具類 XmlUtils.java
juliet.test 存放測試類 StudentTest.java
/java/src/cn/UI/Main.java 存放主程式
xml檔案:src目錄下
/java/src/book.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> <student examid="127" idcard="121"> <name>aa</name> <location>beijing</location> <grade>89.0</grade> </student> <student examid="127" idcard="121"> <name>aa</name> <location>beijing</location> <grade>89.0</grade> </student> <student examid="c" idcard="d"> <name>b</name> <location>e</location> <grade>12.0</grade> </student> </exam>
/java/src/cn/domin/Student.java
package cn.domin; public class Student { private String idcard; private String examid; private String location; private Double grade; private String name; 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 getLocation() { return location; } public void setLocation(String location) { this.location = location; } public Double getGrade() { return grade; } public void setGrade(Double grade) { this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
/java/src/cn/utils/XmlUtils.java
package cn.utils; import java.io.FileNotFoundException; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; 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 { private static String filename="src/example.xml"; public static Document getDocument() throws Exception{ //得到 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(filename); return document; } public static void write2xml(Document document) throws FileNotFoundException, TransformerException{ TransformerFactory tsf = TransformerFactory.newInstance(); Transformer transform=tsf.newTransformer(); transform.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename))); } }
/java/src/cn/dao/StudentDao.java
package cn.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import cn.domin.Student;
import cn.exception.StudentNotExitException;
import cn.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());
//double + ""得到新的字串
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) {
e.printStackTrace();
throw new RuntimeException(e); //轉換成執行時異常 給上一層處理
} //checked 異常 編譯時異常
}
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相匹配的學生 建立學生物件並且返回
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("location").item(0).getTextContent()));
return s;
}
}
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void delete(String name) throws StudentNotExitException{
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 StudentNotExitException(name + "不存在");
} catch (StudentNotExitException e) {
throw e;
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}
/java/src/cn/exception/StudentNotExitException.java
package cn.exception;
public class StudentNotExitException extends Exception {
public StudentNotExitException() {
// TODO Auto-generated constructor stub
}
public StudentNotExitException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public StudentNotExitException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public StudentNotExitException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
}
/java/src/junit/test/StudentTest.java
package junit.test;
import org.junit.Test;
import cn.dao.StudentDao;
import cn.domin.Student;
public class StudentTest {
@Test
public void testadd(){
StudentDao dao = new StudentDao();
Student s = new Student();
s.setExamid("127");
s.setGrade(89.0);
s.setIdcard("121");
s.setLocation("beijing");
s.setName("aa");
dao.add(s);
}
}
/java/src/cn/UI/Main.java
package cn.UI;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import cn.dao.StudentDao;
import cn.domin.Student;
import cn.exception.StudentNotExitException;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
try {
// TODO Auto-generated method stub
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("add sucess!");
}else if("b".equals(type)){
System.out.print("請輸入需要刪除的學生姓名:");
String name = br.readLine();
try {
StudentDao dao = new StudentDao();
dao.delete(name);
System.out.println("刪除成功!");
} catch (StudentNotExitException e) {
System.out.println("您要刪除的使用者不存在!");
}
}else if("c".equals(type)){
}else{
System.out.println("請輸入正確選項");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("sorry one err");
}
}
}