1.xml文件: 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 <n ...
1.xml文件:
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代碼
1 import java.io.File; 2 import java.io.IOException; 3 import java.util.Scanner; 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 //在學生管理系統裡面,學生的學號是唯一的,姓名有可能重覆 21 public class StudentManager { 22 public static void main(String[] args) { 23 try { 24 Document doc = Domutils.getDoc(new File("xml文件的相對路徑")); 25 Scanner input = new Scanner(System.in); 26 System.out.println("歡迎來到學生管理系統\n\n\n請輸入你要進行什麼操作是:\n1.添加學生信息\n2.刪除學生信息\n3.修改學生信息\n(請輸入前邊的序號)"); 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 //修改學生信息 44 private static void updStudent(Document doc) { 45 Element updStudent = null; 46 Scanner input = new Scanner(System.in); 47 System.out.println("請輸入你要修改的學生的學號:"); 48 String studentid = input.nextLine(); 49 System.out.println("請輸入新學生的姓名:"); 50 String newName = input.nextLine(); 51 System.out.println("請輸入新學生的年齡:"); 52 String newAge = input.nextLine(); 53 54 //將每一個學生的列出來,for迴圈判斷你要修改信息的學生是哪一個 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 //對學生的name屬性進行賦新值 60 updStudent.getElementsByTagName("name").item(i).getFirstChild().setNodeValue(newName); 61 //對學生的age 屬性賦新值 62 updStudent.getElementsByTagName("age").item(i).getFirstChild().setNodeValue(newAge); 63 64 }else{ 65 break; 66 } 67 } 68 //找出根元素,將修改後的元素持久化到文件 69 Element root = doc.getDocumentElement(); 70 transform(root); 71 System.out.println(updStudent); 72 } 73 //刪除學生信息 74 private static void delStudent(Document doc) { 75 Scanner input = new Scanner(System.in); 76 //輸入你要刪除的學生的 學號 77 System.out.println("請輸入要刪除學生的學號:"); 78 String studentid = input.nextLine(); 79 Element root = doc.getDocumentElement(); 80 81 //將學生列成一個表,進行遍歷,找對應學號的學生進行刪除 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("沒有該學生"); 91 break; 92 } 93 } 94 //持久化到文件 95 transform(root); 96 } 97 98 //添加學生信息 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 //從控制台輸入 103 Scanner input = new Scanner(System.in); 104 System.out.println("請輸入學生的序號:id = "); 105 106 //將學生放到一個列表裡面,看我們要添加的學生的學號裡面是否已經有了,如果有,需要將新加入的學生的學號改一下 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("該序號學生表裡面已經存在,請重新輸入一個新的序號:"); 112 studentid = input.nextLine(); 113 }else { 114 break; 115 } 116 } 117 118 System.out.println("請輸入要添加學生的姓名:name = "); 119 String name_value = input.nextLine(); 120 System.out.println("請輸入要添加學生的年齡:age = "); 121 String age_value = input.nextLine(); 122 123 //創建節點 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 //關聯節點之間的關係 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 //持久化到文件 137 transform(root); 138 139 } 140 //持久化到文件的方法 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解析文件(將獲取解析文件的部分封裝起來)
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 {
//獲取工廠模式 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//獲取builder對象 DocumentBuilder builder = factory.newDocumentBuilder();
//將要解析文件載入成一個樹狀文件,開始解析 Document document = builder.parse(file); return document; } }