创建一个对象类
对于xml的操作我们需要到一个jar包
dom4j超链接
链接:https://pan.baidu.com/s/1V9a5M4v6uBIGH0SeEQmkmg
提取码:fjva
class Student {
private String name;
private String age;
public Student( String name, String age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
", name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
public Student() {
}
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;
}
}
写一个工具类
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
class StudentUtil {
public File file=new File("E:\\idea代码\\xml\\src\\Student.xml");
//添加
public void add(Student stu) throws DocumentException, IOException {
Document rootFile = getRootFile();
//获取根目录
Element root = rootFile.getRootElement();
//添加元素
Element element = root.addElement("student");
//元素下面的值,id
int id=getid()+1;
element.addAttribute("ID",String.valueOf(id));
//添加姓名
element.addElement("name").setText(stu.getName());
element.addElement("age").setText(stu.getAge());
//关流
close(rootFile);
}
//删除
public void delete (String id) throws DocumentException, IOException {
Document rootFile = getRootFile();
Element root = rootFile.getRootElement();
Element byID = root.elementByID(id);
//推荐先到元素的父目录再去删除此元素
byID.getParent().remove(byID);
close(rootFile);
}
//更改
public void update(String id,Student stu) throws DocumentException, IOException {
Document rootFile = getRootFile();
Element root = rootFile.getRootElement();
Element byID = root.elementByID(id);
byID.element("name").setText(stu.getName());
byID.element("age").setText(stu.getAge());
close(rootFile);
}
//查找
public Student findOne(String id) throws DocumentException {
Student stu = new Student();
Document rootFile = getRootFile();
Element root = rootFile.getRootElement();
Element byID = root.elementByID(id);
stu.setName(byID.elementText("name"));
stu.setAge(byID.elementText("age"));
return stu;
}
//查询所有
public List<Element> findAll() throws DocumentException {
Document rootFile = getRootFile();
Element root = rootFile.getRootElement();
List<Element> list = root.elements();
return list;
}
//获取id
public int getid() throws DocumentException {
//获取数组索引
int i=0;
//获取数组长度
int index=0;
SAXReader saxReader = new SAXReader();
Document read = saxReader.read(file);
Element root = read.getRootElement();
List<Element> elements = root.elements();
for (Element e:elements) {
index++;
}
int []arr=new int[index];
for (Element e:elements) {
//获取student属性
String id = e.attributeValue("ID");
arr[i]= Integer.parseInt(id);
i++;
}
Arrays.sort(arr);
//获取Id的最大值
int id=arr[index-1];
return id;
}
//获取根节点
public Document getRootFile() throws DocumentException {
SAXReader saxReader = new SAXReader();
return saxReader.read(file);
}
//关流
public void close(Document root) throws IOException {
OutputFormat prettyPrint = OutputFormat.createPrettyPrint();
FileWriter fileWriter = new FileWriter(file);
XMLWriter xmlWriter = new XMLWriter(fileWriter, prettyPrint);
xmlWriter.write(root);
xmlWriter.close();
}
}
测试类
import java.util.List;
class StudentTest{
public static void main(String[] args) throws IOException, DocumentException {
StudentUtil studentUtil = new StudentUtil();
studentUtil.add(new Student("周笋","132"));
studentUtil.update("1",new Student("王大锤","23"));
System.out.println( studentUtil.findOne("2"));
List<Element> all = studentUtil.findAll();
for ( Element element:all
) {
System.out.println(element.element("name").getText()+"---"+element.elementText("age"));
}
studentUtil.delete("2");
}
}
xml效果
<?xml version="1.0" encoding="UTF-8"?>
<root>
<student ID="1">
<name>王大锤</name>
<age>23</age>
</student>
<student ID="2">
<name>周笋</name>
<age>132</age>
</student>
<student ID="3">
<name>周笋</name>
<age>132</age>
</student>
</root>
本人小白一枚,所以代码有点冗杂,希望大家多谅解