Student.java
Package Com.bean;
public class Student {
Private String Examid;
Private String Idcard;
private String name;
Private String location;
private double grade;
Public String Getexamid () {
return examid;
}
public void Setexamid (String examid) {
This.examid = Examid;
}
Public String Getidcard () {
return idcard;
}
public void Setidcard (String idcard) {
This.idcard = Idcard;
}
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;
}
}
Studentutil.java
Package com.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;
Import org.w3c.dom.Element;
Import com.bean.Student;
public class Studentutil {
Private static final String file = "Src/student.xml";
Methods of obtaining document documents
public static Document GetDocument () {
try {
Documentbuilderfactory factory = Documentbuilderfactory
. newinstance ();
Documentbuilder db = Factory.newdocumentbuilder ();
return db.parse (file);
catch (Exception e) {
throw new RuntimeException (e);
}
}
Ways to write XML files
public static void WriteXml (document document) {
try {
Transformer tf = Transformerfactory.newinstance (). Newtransformer ();
Tf.transform (new Domsource (document), new Streamresult (file));
catch (Exception e) {
throw new RuntimeException (e);
}
}
Encapsulate the value of a node in a bean
public static Student Nodebean (Element e,class<student> c) {
try {
Student s = c.newinstance ();
S.setexamid (E.getattribute ("Examid"));
S.setidcard (E.getattribute ("Idcard"));
S.setname (E.getelementsbytagname ("name"). Item (0). Gettextcontent ());
S.setlocation (E.getelementsbytagname ("location"). Item (0). Gettextcontent ());
S.setgrade (Double.parsedouble (E.getelementsbytagname ("Grade"). Item (0). Gettextcontent ());
return s;
catch (Exception E1) {
throw new RuntimeException (E1);
}
}
}
Studentdao.java
Package Com.dao;
Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import org.w3c.dom.NodeList;
Import com.bean.Student;
Import Com.utils.StudentUtil;
public class Studentdao {
/**
* @param args
*/
Increase student
public void inserts (Student s) {
Document document = Studentutil.getdocument ();
Creating Sutudet Nodes
Element RootNode = document.createelement ("student");
Set properties for Sutudet node
Rootnode.setattribute ("Examid", S.getexamid ());
Rootnode.setattribute ("Idcard", S.getidcard ());
To create a Sutudet child node
Element name = document.createelement ("name");
Element location = document.createelement ("location");
Element grade = Document.createelement ("Grade");
Set a value to a sutudet child node
Name.settextcontent (S.getname ());
Location.settextcontent (S.getlocation ());
Grade.settextcontent (S.getgrade () + "");
Append Sutudet child nodes to the node
Rootnode.appendchild (name);
Rootnode.appendchild (location);
Rootnode.appendchild (grade);
Add the Sutudet node to the root node
document.getElementsByTagName ("Exam"). Item (0). appendchild (RootNode);
Write to XML file
Studentutil.writexml (document);
}
Delete Student
public void Deletes (String name) {
Document document = Studentutil.getdocument ();
NodeList list = document.getElementsByTagName ("name");
for (int i=0;i<list.getlength (); i++) {
element node = (element) list.item (i);
if (Node.gettextcontent (). Equals (name)) {
Node.getparentnode (). Getparentnode (). RemoveChild (Node.getparentnode ());
Studentutil.writexml (document);
Return
}
}
}
Query students
Public Student Querys (String examid) {
Document document = Studentutil.getdocument ();
NodeList list = document.getElementsByTagName ("student");
for (int i=0;i<list.getlength (); i++) {
Element Studentnode = (Element) list.item (i);
String value = Studentnode.getattribute ("Examid");
if (Value.equals (Examid)) {
Student Student = Studentutil.nodebean (Studentnode, Student.class);
return student;
}
}
return null;
}
}
Studenttest.java
Package Com.juil;
Import Org.junit.Test;
Import com.bean.Student;
Import Com.dao.StudentDao;
public class Studenttest {
/**
* @param args
*/
Private Student Student = new Student ();
Private Studentdao DAO = new Studentdao ();
Test Add Student
@Test
public void Inserttest () {
Student.setexamid ("12345");
Student.setidcard ("1122");
Student.setname ("Yang");
Student.setlocation ("Xingtai");
Student.setgrade (100);
Dao.inserts (student);
}
@Test
public void Querytest () {
Dao.querys ("111");
System.out.println (Student.getname ());
}
@Test
public void Deletetest () {
Dao.deletes ("Li Xiaosan");
}
}
Main.java
Package com.main;
Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import com.bean.Student;
Import Com.dao.StudentDao;
public class Main {
public static void Main (string[] args) {
while (true) {
try {
System.out.println ("Add student (a) find student (b) Delete student (c) exit (quit)");
System.out.print ("Please select the type of operation:");
BufferedReader br = new BufferedReader (New InputStreamReader (
system.in));
String type = Br.readline ();
if ("Quit". Equalsignorecase (Type)) {
Return
}
if (!type.matches ("[Abcabc]")) {
System.out.println ("Please enter the correct type of operation ..."). ");
Continue
}
if (Type.equalsignorecase ("a")) {
Go to add student link
try {
System.out.print ("Please enter student name:");
String name = Br.readline ();
System.out.print ("Please enter student Ticket number:");
String Examid = Br.readline ();
System.out.print ("Please enter student ID number:");
String Idcard = Br.readline ();
System.out.print ("Please enter student location:");
String location = Br.readline ();
System.out.print ("Please input student score:");
Double grade = Double.parsedouble (Br.readline ());
Student s = new Student ();
S.setexamid (Examid);
S.setgrade (grade);
S.setidcard (Idcard);
S.setlocation (location);
S.setname (name);
Studentdao dao = new Studentdao ();
Dao.inserts (s);
System.out.println ("Data entry succeeded ... ");
} catch (Exception e) {
System.out.println ("input failed due to unknown reason ..."). ");
}
} else if (Type.equalsignorecase ("B")) {
//Enter the lookup link
try {
System.out.print ("Please enter student Ticket number:");
String Examid = Br.readline ();
Student s = new Student ();
S.setexamid (Examid);
Studentdao dao = new Studentdao ();
S = Dao.querys (Examid);
System.out.println ("Name:" + s.getname ());
System.out.println ("Ticket Number:" + s.getexamid ());
System.out.println ("School Number:" + s.getidcard ());
System.out.println ("Location:" + s.getlocation ());
System.out.println ("Score:" + S.getgrade ());
System.out.println ("Find successful!!!");
} catch (RuntimeException e) {
System.out.println ("Query failed for unknown reason ..."). ");
}
Else {
//Enter Delete link
try {
System.out.print ("Enter student Name:");
String name = Br.readline ();
Student s = new Student ();
S.setname (name);
Studentdao dao = new Studentdao ();
Dao.deletes (name);
System.out.println ("delete successful!!!");
} catch (RuntimeException e) {
System.out.println ("Failed to delete due to unknown reason ..."). ");
}
}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("System is wrong, please try again ...") ");
}
}
}
}
Student.xml
<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?><exam>
<student examid= "222" idcard= ">"
<name> John </name>
<location> Shenyang </location>
<grade>89</grade>
</student>
<student examid= "444" idcard= "333" >
<name> Dick </name>
<location> Dalian </location>
<grade>97</grade>
</student>
<student examid= "" idcard= "222" >
<name>xxx</name>
<location> Beijing </location>
<grade>45.0</grade>
</student>
<student examid= "12345" idcard= "1122" >
<name> Yang </name>
<location> Xingtai </location>
<grade>100.0</grade>
</student>
</exam>