How to use the xml file as a database for students to add, delete, modify and check
How to use the xml file as a database for students to add, delete, change and search, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1.xml file:
Ttt 44 linda2 22 linda3 23 jack 2 yyh2 22
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 student number is unique, the name may repeat public class StudentManager {public static void main (String [] args) {try {Document doc = Domutils.getDoc ("relative path to the xml file"); Scanner input = new Scanner (System.in) System.out.println ("Welcome to the student management system\ n\ n\ nPlease enter what you want to do:\ N1. Add student information\ n2. Delete student information\ n3. Modify student information\ n (please enter the previous serial 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 number you want to modify:"); 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 (); / / list each student, and the for loop determines which student you want to modify the information is NodeList list = doc.getElementsByTagName ("student"); for (int I = 0; I)