1 、创建java项目,引入dom4j-1.6.1.jar
2 、在java项目中定义一个xml文档,命名为 student.xml,放在conf文件夹下。
初始内容如下(包含一个students的根标签)
<?xml version="1.0" encoding="UTF-8"?>
<students>
</students>
3 、定义student类,含有姓名,年龄,是否为男生 三个属性
package fileoperation;
public class Student {
private String name;
private int age;
private boolean isBoy;
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 boolean isBoy() {
return isBoy;
}
public void setBoy(boolean isBoy) {
this.isBoy = isBoy;
}
@Override
public String toString() {
return "student [name=" + name + ", age=" + age + ", isBoy=" + isBoy + "]";
}
}
4 、定义对xml文件进行增删改查操作的类
package fileoperation;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Operation {
private static String FILE_PATH = "conf/student.xml"; //文件目录
//在xml文件中添加一个学生信息
public static void addStudent(Student student)
{
InputStream in = null;
SAXReader reader = new SAXReader();
Document doc = null;
try
{
in = new FileInputStream(FILE_PATH);
System.out.println(in);
doc = reader.read(in);
Element root = doc.getRootElement(); //获取xml根节点,即students节点
Element element = root.addElement("student");
element.addElement("name").addText(student.getName());
element.addElement("age").addText(String.valueOf(student.getAge()));
element.addElement("isBoy").addText(String.valueOf(student.isBoy()));
XMLWriter writer = new XMLWriter();
FileOutputStream fos = new FileOutputStream(FILE_PATH);
System.out.println(fos);
writer.setOutputStream(fos);
writer.write(doc);
writer.close();
}
catch (Exception e)
{
System.out.println("error");
}
finally
{
try
{
if(in != null)
in.close();
}
catch (IOException e)
{
System.out.println("error");
}
}
}
//更新学生信息,这里我们假设名称相同则为同一学生
public static void updateStudent(Student student)
{
InputStream in = null;
SAXReader reader = new SAXReader();
Document doc = null;
try
{
in = new FileInputStream(FILE_PATH);
doc = reader.read(in);
Element root = doc.getRootElement();
for (Iterator it = root.elementIterator(); it.hasNext();)
{
Element element = (Element) it.next();
if (student.getName().equals(element.element("name").getText()))
{
element.element("age").setText(String.valueOf(student.getAge()));
element.element("isBoy").setText(String.valueOf(student.isBoy()));
break;
}
}
FileWriter writer = new FileWriter(FILE_PATH);
doc.write(writer);
writer.flush();
writer.close();
}
catch (Exception e)
{
System.out.println("error");
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
System.out.println("error");
}
}
}
//列出xml中所有学生信息
public static List<Student> getStudents()
{
InputStream in = null;
SAXReader reader = new SAXReader();
Document doc = null;
List<Student> Students = new ArrayList<>();
try
{
in = new FileInputStream(FILE_PATH);
doc = reader.read(in);
Element root = doc.getRootElement();
List<Element> elements = root.elements();
Students = new ArrayList<Student>();
for (Element element : elements)
{
Student student = new Student();
student.setName(element.elementText("name"));
student.setAge(Integer.valueOf(element.elementText("age")));
student.setBoy(Boolean.valueOf(element.elementText("isBoy")));
Students.add(student);
}
}
catch (Exception e1)
{
System.out.println("error");
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
System.out.println("error");
}
}
return Students;
}
//判断某个学生是否存在该xml中
public static boolean studentIsExist(String name)
{
InputStream in = null;
SAXReader reader = new SAXReader();
Document doc = null;
try {
in = new FileInputStream(FILE_PATH);
doc = reader.read(in);
Element root = doc.getRootElement();
List<Element> elements = root.elements();
for (Element element : elements)
{
if(name != null && name.equals(element.elementText("name")))
return true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
return false;
}
//删除xml文件中的所有学生信息,只剩下初始的xml文件
public static boolean deleteAllStudents()
{
InputStream in = null;
SAXReader reader = new SAXReader();
Document doc = null;
try {
in = new FileInputStream(FILE_PATH);
doc = reader.read(in);
Element root = doc.getRootElement();
List<Element> elements = root.elements();
for(Element element : elements)
{
element.detach();
}
FileWriter writer = new FileWriter(FILE_PATH);
doc.write(writer);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
//删除xml中某个学生的信息
public static boolean deleteStudent(String name)
{
InputStream in = null;
SAXReader reader = new SAXReader();
Document doc = null;
try {
in = new FileInputStream(FILE_PATH);
doc = reader.read(in);
Element root = doc.getRootElement();
List<Element> elements = root.elements();
for(Element element : elements)
{
if(name != null && name.equals(element.elementText("name")))
element.detach();
}
FileWriter writer = new FileWriter(FILE_PATH);
doc.write(writer);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}