stu.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <exam> <student> <name>张三</name> <location>西安</location> <grade>98</grade> </student> <student> <name>李四</name> <location>临潼</location> <grade>59</grade> </student> </exam>
CRUD.java
package XMLUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import com.student.Student; import com.student.StudentNotFoundException; public class CRUD { public static void create(Student student) throws Exception { Document dom =Utils.createDocument(); Element student_tag=dom.createElement("student"); Element name_tag=dom.createElement("name"); name_tag.setTextContent(student.getName()); Element location_tag=dom.createElement("location"); location_tag.setTextContent(student.getLocation()); Element grade_tag=dom.createElement("grade"); grade_tag.setTextContent(student.getGrade()+""); student_tag.appendChild(name_tag); student_tag.appendChild(location_tag); student_tag.appendChild(grade_tag); dom.getFirstChild().appendChild(student_tag); Utils.UpdateXML(dom); } public static Student Read(String name) throws Exception { Document dom =Utils.createDocument(); NodeList list=dom.getElementsByTagName("name"); for(int k=0;k<list.getLength();++k) { if(list.item(k).getTextContent().equals(name)) { Element student=(Element) list.item(k).getParentNode(); Student st=new Student(); st.setGrade(Integer.parseInt(student.getElementsByTagName("grade").item(0).getTextContent())); st.setLocation(student.getElementsByTagName("location").item(0).getTextContent()); st.setName(name); return st; } } throw new StudentNotFoundException("對不起,未找到該同學"); } public static void Delete(String name) throws Exception { Document dom =Utils.createDocument(); NodeList list=dom.getElementsByTagName("name"); for(int k=0;k<list.getLength();++k) { if(list.item(k).getTextContent().equals(name)) { Element student=(Element) list.item(k).getParentNode(); student.getParentNode().removeChild(student ); Utils.UpdateXML(dom); return ; } } throw new StudentNotFoundException("對不起,未找到該同學"); } }
student.java
package com.student; public class Student { private String name; private String location; private int grade; public Student(String na,String lo,int gr) { name=na; location=lo; grade=gr; } public Student() { } 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 int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } }
Utils.java
package XMLUtils; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Utils { public static Document createDocument() throws Exception { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); return builder.parse("src/stu.xml");//注意格式 } public static void UpdateXML(Document dom) throws TransformerException,FileNotFoundException { TransformerFactory tf=TransformerFactory.newInstance(); Transformer tr=tf.newTransformer(); tr.transform(new DOMSource(dom),new StreamResult(new FileOutputStream("src/stu.xml") )); } }
test_CRUD.java 用来测试CRUD的每个功能的。
package Test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.w3c.dom.Document; import XMLUtils.CRUD; import XMLUtils.Utils; import com.student.Student; public class test_CRUD { /** * @param args * @throws IOException */ public static void main(String[] args) { try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String name,location,grade; System.out.print("请输入姓名:"); name=br.readLine(); CRUD.Delete(name); System.out.println("已成功删除"); }catch(Exception ex) { ex.printStackTrace(); System.out.println("对不起,业务繁忙,请重新输入."); } } }
StudentNotFoundException.java
package com.student; public class StudentNotFoundException extends Exception { public StudentNotFoundException() { // TODO Auto-generated constructor stub } public StudentNotFoundException(String message) { super(message); // TODO Auto-generated constructor stub } public StudentNotFoundException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } public StudentNotFoundException(String message,Throwable cause) { super(message,cause); // TODO Auto-generated constructor stub } public StudentNotFoundException(String message,Throwable cause,boolean enableSuppression,boolean writableStackTrace) { super(message,cause,enableSuppression,writableStackTrace); // TODO Auto-generated constructor stub } }
有一点值得注意:
在查阅某个学生时,找到了学生标签,然后查找姓名,地址,成绩标签时,不是查看其属性(属性是修饰标签的),而是要调用student_tag.getElementsByTagName("tagname");来获取这个学生的子标签。document也相当于一个标签,它有getElementsByTagName的方法,而其它标签也有此标签也就不奇怪了吧。
原文链接:https://www.f2er.com/xml/294884.html