导入dom4j的jar包,这个上网搜jar包就可以免费下载滴
新建三个包,
在Student JAVA文件中
添加操作
package cn.xiaozhang.vo;
public class Student {
private String id;
private String name;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
在StuService Java文件中
package cn.xiaozhang.stu;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import Dom4jUtils.Dom4jUtils;
import cn.xiaozhang.vo.Student;
public class StuService {
//增加
public static void addstu(Student student) throws Exception{
//括号里面本来是参数,当参数很多的的时候,可以使用类的对象进行操作
/*
*创建解析器
*得到document
*获取根节点
*在根节点上面创建stu标签
*在stu标签上面依次添加值
*在ID name age上面依次添加值
*回写
* */
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(Dom4jUtils.PATH);
Element root = document.getRootElement();
Element stu = root.addElement("student");