1.xml file:
<?xml version= "1.0" encoding= "UTF-8"?><students> <student id=
"2" >
<name>ttt</ name>
<age>44</age>
</student>
<student id= "3" >
<name>linda2 </name>
<age>22</age>
</student>
<student id= "4" >
<name> linda3</name>
<age>23</age>
</student>
<student id= "5" >
< name>jack</name>
<age>2</age>
</student>
<student id= "1" >
<name>yyh1</name>
<age>22</age>
</student>
</Students>
2.Java Code
Import Java.io.File;
Import java.io.IOException;
Import Java.util.Scanner;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.transform.Transformer;
Import javax.xml.transform.TransformerConfigurationException;
Import javax.xml.transform.TransformerException;
Import Javax.xml.transform.TransformerFactory;
Import Javax.xml.transform.TransformerFactoryConfigurationError;
Import Javax.xml.transform.dom.DOMSource;
Import Javax.xml.transform.stream.StreamResult;
Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import org.w3c.dom.NodeList;
Import Org.w3c.dom.Text;
Import org.xml.sax.SAXException; In the student management system, the student's school number is unique, the name may repeat public class Studentmanager {public static void main (string[] args) {try {Doc
Ument doc = domutils.getdoc (new file ("Relative path of XML file"));
Scanner input = new Scanner (system.in);
System.out.println ("Welcome to the Student Management system \n\n\n Please enter what you want to do is: \n1." Add student information \n2. Delete Student information \n3. Modify student information \ n (Please enter the ordinal number) ");
int num = Input.nextint (); if (num == 1) {addstudent (doc);
}else if (num = 2) {delstudent (doc);
}else if (num = 3) {updstudent (doc);
} catch (Saxexception e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
catch (Parserconfigurationexception e) {e.printstacktrace ();
}///modify student information private static void Updstudent (Document doc) {Element updstudent = null;
Scanner input = new Scanner (system.in);
System.out.println ("Please enter the student's number you want to revise:");
String StudentID = Input.nextline ();
System.out.println ("Please enter the name of the new student:");
String newName = Input.nextline ();
System.out.println ("Please enter the age of the new student:");
String newage = Input.nextline ();
Make a list of each student, for loops to determine which of the students you want to modify the information nodelist list = doc.getelementsbytagname ("student"); for (int i = 0; I <list.getlength (); i++) {if studentid.equals (List.item (i). GetAttributes (). getNamedItem ("id"). GE Tnodevalue ())) {updstudent = (Element) doc.getelemenTsbytagname ("Student"). Item (i). Getfirstchild (). Getparentnode ();
Assigns a new value to the student's Name property updstudent.getelementsbytagname ("name"). Item (i). Getfirstchild (). setNodeValue (NewName);
Assign a new value Updstudent.getelementsbytagname ("age") to the student's Age property. Item (i). Getfirstchild (). setNodeValue (NewAge);
}else{break;
}//Find the root element to persist the modified element to the file element root = Doc.getdocumentelement ();
Transform (root);
System.out.println (updstudent);
///Delete student information private static void Delstudent (Document doc) {Scanner input = new Scanner (system.in);
Enter the student number you want to delete System.out.println ("Please enter the student number to be deleted:");
String StudentID = Input.nextline ();
Element root = Doc.getdocumentelement ();
The students are listed as a table, traverse, find the corresponding student number to delete nodelist list = doc.getelementsbytagname ("student"); for (int i = 0; i < list.getlength (); i++) {if (StudentID). Equals (List.item (i). GetAttributes (). getNamedItem ("id"). g Etnodevalue ()) {Element delstudent =(Element) doc.getelementsbytagname ("Student"). Item (i). Getfirstchild (). Getparentnode ();
Root.removechild (delstudent);
Break
}else {System.out.println ("No such student");
Break
}//persisted to file transform (root); ///Add student information private static void Addstudent (Document doc) {//System.out.println (Doc.getelementsbytagname ("Studen
T "). Item (1). GetAttributes (). getNamedItem (" id "). getnodevalue ());
Element root = Doc.getdocumentelement ();
Enter Scanner input = new Scanner (system.in) from the console;
System.out.println ("Please enter the student's serial number: id =");
Put the students into a list, to see if we want to add the student's number inside whether there is, if there is, need to add new students to change the study number nodelist list = doc.getelementsbytagname ("student");
String StudentID = Input.nextline (); for (int i = 0; i < list.getlength (); i++) {if studentid.equals (List.item (i). GetAttributes (). getNamedItem ("id"). g
Etnodevalue ()) {System.out.println ("This number is already in the student table, please re-enter a new serial number:");
StudentID = Input.nextline (); }else {break;
} System.out.println ("Enter the name of the student you want to add: Name =");
String Name_value = Input.nextline ();
System.out.println ("Please enter the age of the student to be added: ages =");
String Age_value = Input.nextline ();
Create node Element student = doc.createelement ("student");
Element name = doc.createelement ("name");
Element age = doc.createelement (' age ');
Text Namtext = Doc.createtextnode (Name_value);
Text Agetext = Doc.createtextnode (Age_value);
The relationship between the associated nodes Root.appendchild (student);
Student.appendchild (name);
Student.appendchild (age);
Student.setattribute ("id", StudentID);
Name.appendchild (Namtext);
Age.appendchild (Agetext);
Persisted to file transform (root); }//persisted to the file method private static void transform (Element root) throws Transformerfactoryconfigurationerror {Tran
Sformerfactory factory = Transformerfactory.newinstance ();
try {Transformer tf = Factory.newtransformer (); Tf.transform (New Domsource (ROOT), new Streamresult (New File ("Src/com/briup/dom/student.xml"));
catch (Transformerconfigurationexception e) {e.printstacktrace ();
catch (Transformerexception e) {e.printstacktrace (); }
}
}
2.Dom Parse file (encapsulates the part that gets the parsing file)
Import Java.io.File;
Import java.io.IOException;
Import Java.nio.file.attribute.AclEntry.Builder;
Import Javax.xml.parsers.DocumentBuilder;
Import javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;
Import org.w3c.dom.Document;
Import org.xml.sax.SAXException;
public class Domutils {public
static Document getdoc (file file) throws Saxexception, IOException, Parserconfiguratio nexception {
//Get Factory mode
documentbuilderfactory factory =
documentbuilderfactory.newinstance ();
Gets the Builder object
Documentbuilder builder = Factory.newdocumentbuilder ();
The parsing file is loaded into a tree file, and begins parsing document
document = Builder.parse (file);
return document;
}
The above will be the XML file as a small database, the students to add or delete the simple example is to share the whole of the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.