Use xml files as a small database to add, delete, modify, and query students, and add, delete, and delete xml files.

Source: Internet
Author: User

Use xml files as a small database to add, delete, modify, and query students, and add, delete, and delete xml files.

1. xml file:

 1 <?xml version="1.0" encoding="UTF-8"?><Students> 2   <student id="2"> 3     <name>ttt</name> 4     <age>44</age> 5   </student> 6   <student id="3"> 7     <name>linda2</name> 8     <age>22</age> 9   </student>10   <student id="4">11     <name>linda3</name>12     <age>23</age>13   </student>14   <student id="5">15     <name>jack</name>16     <age>2</age>17   </student>18   <student id="1">19       <name>yyh1</name>20       <age>22</age>21   </student>22 </Students>

 

2. Java code

1 import java. io. file; 2 import java. io. IOException; 3 import java. util. export; 4 5 import javax. xml. parsers. parserConfigurationException; 6 import javax. xml. transform. transformer; 7 import javax. xml. transform. transformerConfigurationException; 8 import javax. xml. transform. transformerException; 9 import javax. xml. transform. transformerFactory; 10 import javax. xml. transform. transformerFactoryConfigurationError; 11 import javax. xml. transform. dom. DOMSource; 12 import javax. xml. transform. stream. streamResult; 13 14 import org. w3c. dom. document; 15 import org. w3c. dom. element; 16 import org. w3c. dom. nodeList; 17 import org. w3c. dom. text; 18 import org. xml. sax. SAXException; 19 20 // In the student management system, the student's student ID is unique and the name may be repeated 21 public class StudentManager {22 public static void main (String [] args) {23 try {24 Document doc = Domutils. getDoc (new File ("relative path of xml File"); 25 bytes input = new bytes (System. in); 26 System. out. println ("Welcome to student management system \ n please enter what you want to do: \ n1. add student information \ n2. Delete student information \ n3. modify student information \ n (enter the number of the front edge) "); 27 int num = input. nextInt (); 28 if (num = 1) {29 addStudent (doc); 30} else if (num = 2) {31 delStudent (doc ); 32} else if (num = 3) {33 updStudent (doc); 34} 35} catch (SAXException e) {36 e. printStackTrace (); 37} catch (IOException e) {38 e. printStackTrace (); 39} catch (ParserConfigurationException e) {40 e. printStackTrace (); 41} 42} 43 // modify student information 44 private static void updStudent (Document doc) {45 Element updStudent = null; 46 partition input = new partition (System. in); 47 System. out. println ("Enter the student ID you want to modify:"); 48 String studentid = input. nextLine (); 49 System. out. println ("enter the name of the new student:"); 50 String newName = input. nextLine (); 51 System. out. println ("Enter the age of the new student:"); 52 String newAge = input. nextLine (); 53 54 // list each student. The for loop determines which student you want to modify the information is 55 NodeList list = doc. getElementsByTagName ("student"); 56 for (int I = 0; I <list. getLength (); I ++) {57 if (studentid. equals (list. item (I ). getAttributes (). getNamedItem ("id "). getNodeValue () {58 updStudent = (Element) doc. getElementsByTagName ("student "). item (I ). getFirstChild (). getParentNode (); 59 // assign a value of 60 updStudent to the student's name attribute. getElementsByTagName ("name "). item (I ). getFirstChild (). setNodeValue (newName); 61 // assign a value 62 updStudent to the student's age attribute. getElementsByTagName ("age "). item (I ). getFirstChild (). setNodeValue (newAge); 63 64} else {65 break; 66} 67} 68 // locate the root Element and persistently Save the modified Element to the 69 Element root = doc file. getDocumentElement (); 70 transform (root); 71 System. out. println (updStudent); 72} 73 // Delete student information 74 private static void delStudent (Document doc) {75 student input = new student (System. in); 76 // enter the student ID 77 System. out. println ("Enter the student ID to delete:"); 78 String studentid = input. nextLine (); 79 Element root = doc. getDocumentElement (); 80 81 // list the students into a table and traverse the table. Find the students corresponding to the student ID and delete 82 NodeList list = doc. getElementsByTagName ("student"); 83 for (int I = 0; I <list. getLength (); I ++) {84 if (studentid ). equals (list. item (I ). getAttributes (). getNamedItem ("id "). getNodeValue () {85 Element delStudent = (Element) doc. getElementsByTagName ("student "). item (I ). getFirstChild (). getParentNode (); 87 root. removeChild (delStudent); 88 break; 89} else {90 System. out. println ("no student"); 91 break; 92} 93} 94 // persists to file 95 transform (root ); 96} 97 98 // Add student information 99 private static void addStudent (Document doc) {100 // System. out. println (doc. getElementsByTagName ("student "). item (1 ). getAttributes (). getNamedItem ("id "). getNodeValue (); 101 Element root = doc. getDocumentElement (); 102 // enter 103 bytes input = new bytes (System. in); 104 System. out. println ("Enter the student id ="); 105 106 // put the student into a list to see if the student id we want to add already exists, if yes, you need to change the student ID to 107 NodeList list = doc. getElementsByTagName ("student"); 108 String studentid = input. nextLine (); 109 for (int I = 0; I <list. getLength (); I ++) {110 if (studentid. equals (list. item (I ). getAttributes (). getNamedItem ("id "). getNodeValue () {111 System. out. println ("this number already exists in the student table. Please enter a new number:"); 112 studentid = input. nextLine (); 113} else {114 break; 115} 116 117 System. out. println ("enter the name of the student to be added: name ="); 119 String name_value = input. nextLine (); 120 System. out. println ("Enter the age of the student to be added: age ="); 121 String age_value = input. nextLine (); 122 123 // create node 124 Element student = doc. createElement ("student"); 125 Element name = doc. createElement ("name"); 126 Element age = doc. createElement ("age"); 127 Text namText = doc. createTextNode (name_value); 128 Text ageText = doc. createTextNode (age_value); 129 // relationship between associated nodes: 130 root. appendChild (student); 131 student. appendChild (name); 132 student. appendChild (age); 133 student. setAttribute ("id", studentid); 134 name. appendChild (namText); 135 age. appendChild (ageText); 136 // persists to file 137 transform (root); 138 139} 140 // method of persistence to file 141 private static void transform (Element root) 142 throws TransformerFactoryConfigurationError {143 TransformerFactory factory = TransformerFactory. newInstance (); 144 try {145 Transformer tf = factory. newTransformer (); 146 tf. transform (new DOMSource (root), new StreamResult (new File ("src/com/briup/dom/student. xml "); 147} catch (TransformerConfigurationException e) {148 e. printStackTrace (); 149} catch (TransformerException e) {150 e. printStackTrace (); 151} 152} 153}

2. Dom parsing file (encapsulate the part of the obtained 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, ParserConfigurationException {
// Obtain the factory mode DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance ();
// Obtain the builder object DocumentBuilder builder = factory. newDocumentBuilder ();
// Load the file to be parsed into a tree file and start parsing Document document = builder. parse (file); return Document ;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.