xml+dom4j+xpath学生管理系统
生活随笔
收集整理的這篇文章主要介紹了
xml+dom4j+xpath学生管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?xml version="1.0" encoding="UTF-8"?><student> <stu> <name>張三</name> <age>16</age> <sex>男</sex> </stu> <stu> <name>李四</name> <age>18</age> <sex>男</sex> </stu> <stu> <name>王五</name> <age>17</age> <sex>男</sex> </stu>
</student> package cn.itcast.vo;public class Student {private String name;private int age;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String toString(){return name+":"+age+":"+sex;}
} package student;import java.io.FileOutputStream;
import java.util.List;import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;import cn.itcast.vo.Student;public class StuServer {//增加public static void addStu(Student s) throws Exception{SAXReader sax = new SAXReader();Document document = sax.read("src/student.xml");Element root = document.getRootElement();Element stu = root.addElement("stu");Element name = stu.addElement("name");Element age = stu.addElement("age");Element sex = stu.addElement("sex");name.setText(s.getName());age.setText(s.getAge()+"");sex.setText(s.getSex());//回寫OutputFormat format = OutputFormat.createPrettyPrint();XMLWriter xmlwriter = new XMLWriter(new FileOutputStream("src/student.xml"),format);xmlwriter.write(document);xmlwriter.close();}//刪除 根據學生的idpublic static void delStu(String name)throws Exception{SAXReader sax = new SAXReader();Document document = sax.read("src/student.xml");List<Node> list=document.selectNodes("//name");for(Node node:list){String namev = node.getText();if(namev.equals(name)){Element stu = node.getParent();Element student = stu.getParent();student.remove(stu);}}//回寫OutputFormat format = OutputFormat.createPrettyPrint();XMLWriter xmlwriter = new XMLWriter(new FileOutputStream("src/student.xml"),format);xmlwriter.write(document);xmlwriter.close();}public static Student selectStu(String name)throws Exception{SAXReader sax = new SAXReader();Document document = sax.read("src/student.xml");List<Node>list = document.selectNodes("//name");Student s = new Student();for(Node node : list){String namev = node.getText();if(namev.equals(name)){Element stu = node.getParent();int agev =Integer.parseInt(stu.element("age").getText());String sexv = stu.element("sex").getText();s.setAge(agev);s.setName(namev);s.setSex(sexv);}}return s;}
} package cn.itcast.test;import cn.itcast.vo.Student;
import student.StuServer;public class Test {public static void main(String[] args) throws Exception{
// addTest();
// delTest();selectTest();}public static void addTest() throws Exception{Student s = new Student();s.setName("tom");s.setAge(20);s.setSex("男");StuServer.addStu(s);}public static void delTest()throws Exception{StuServer.delStu("tom");}public static void selectTest()throws Exception{Student student = StuServer.selectStu("王五");System.out.println(student.toString());}}
轉載于:https://www.cnblogs.com/wangyinxu/p/7402316.html
總結
以上是生活随笔為你收集整理的xml+dom4j+xpath学生管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python之路_计算机编码及运算符介绍
- 下一篇: Action Service Dao三层