dom4j CURD

Love The Way You Lie 2021-09-11 00:34 391阅读 0赞

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import cn.itcast.xml.example3.domain.Student;

public class StudentDao {
//根据编号更新学员信息
public void update(Student student) throws Exception {
Document document = getDocument();
String xpath = “//student[@id=’”+student.getId()+”‘]“;
Element element = (Element) document.selectSingleNode(xpath);
if(element!=null){
element.element(“name”).setText(student.getName());
element.element(“age”).setText(student.getAge());
write2xml(document);
}else{
System.out.println(“查无此学员”);
}

}
//根据编号显示学员信息
public void read(String id) throws Exception{
Document document = getDocument();
String xpath = “//student[@id=’”+id+”‘]“;
Element element = (Element) document.selectSingleNode(xpath);
if(element!=null){
System.out.println(“编号:” + element.attributeValue(“id”));
System.out.println(“姓名:” + element.elementText(“name”));
System.out.println(“年龄:” + element.elementText(“age”));
}else{
System.out.println(“查无此学员”);
}
}
//根据编号删除某位学员的信息
public void delete(String id) throws Exception{
Document document = getDocument();
String xpath = “//student[@id=’”+id+”‘]“;
Element element = (Element) document.selectSingleNode(xpath);
if(element!=null){
element.getParent().remove(element);
write2xml(document);
}else{
System.out.println(“查无此学员”);
}
}
//增加学员的信息
public boolean create(Student student) throws Exception{
boolean flag = false;
if(student!=null){
Document document = null;
try {
document = getDocument();
} catch (Exception e) {
//创建空XML文件
document = DocumentHelper.createDocument();
//创建根元素
document.addElement(“students”);
}
Element rootElement = document.getRootElement();
Element studentElement = rootElement.addElement(“student”);
studentElement.addAttribute(“id”,student.getId());
studentElement.addElement(“name”).setText(student.getName());
studentElement.addElement(“age”).setText(student.getAge());
write2xml(document);
flag = true;
}
return flag;
}
//将内存中的Document写到硬盘
private void write2xml(Document document) throws Exception {
OutputFormat format = OutputFormat.createPrettyPrint();
OutputStream os = new FileOutputStream(“src/cn/itcast/xml/example3/db/students.xml”);
XMLWriter xmlWriter = new XMLWriter(os,format);
xmlWriter.write(document);
xmlWriter.close();
}
//取得Document对象
private Document getDocument() throws Exception {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(“src/cn/itcast/xml/example3/db/students.xml”));
return document;
}

}

发表评论

表情:
评论列表 (有 0 条评论,391人围观)

还没有评论,来说两句吧...

相关阅读

    相关 dom4j

    一、dom4j常用操作类:   1、SAXReader解析器:org.dom4j.io.SAXReader包;     构造:new SAXReader();     核

    相关 Dom4j解析xml

     dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的

    相关 dom4j使用

    引用大神的,特此加上原文链接:[http://blog.csdn.net/chenweitang123/article/details/6255108\\_Toc2880844

    相关 dom4j解析xml

    前言 随着json的出现,xml的使用越来越少了,尤其是springboot的出现,让xml在项目中几乎销声匿迹了。 不过,有的时候我们可能也是需要用到它的。 博主在

    相关 dom4j解析

    1、XML解析概述      当被数据存储在xml后,我们就希望通过程序获得XML的内容。如果我们使用Java基础所学习的IO知识是可以完成的,不过需要非常繁琐的操作才可