先看一下XML文档

<?xml version="1.0" encoding="gb2312"?>
<exam>
<student idcard="111" examid="222">
<name>张三</name>
<location>深圳</location>
<score>89</score>
</student>
<student idcard="777" examid="666">
<name>李四</name>
<location>上海</location>
<score>99</score>
</student>
</exam>

主要是对此文档读取,添加,删除,保存(刷新)等处理实现下面的功能

首先要建立一个学生的实体类

 public class Student {
private String name;
private String examid;//准考证号
private String idcard;//身份证号
private String location;//地址
private String score; //分数 public Student(){ //无参构造
} public Student(String name, String examid, String idcard, String location,
String score) { //有参构造
super();
this.name = name;
this.examid = examid;
this.idcard = idcard;
this.location = location;
this.score = score;
}
//下面是set get 方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getExamid() {
return examid;
}
public void setExamid(String examid) {
this.examid = examid;
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
} }//类的

然后建立一个接口类,里面添加的方法分别是(添加,删除,读取)

 public interface AddStu {
/**
* 添加学生
*/
public boolean addStudent(Student stu);
/**
* 删除学生
*/
public boolean removeStudent(String name);
/**
* 查询学生
*/
public Student caxunStudent(String examid);
}

然后是实现接口的类

 public class AddStuimpl implements AddStu{

     @Override
public boolean addStudent(Student stu) {
boolean flay=false;
Document dom=null;
//1.创建解析器
SAXReader reader = new SAXReader();
//2.解析文档
try {
dom = reader.read(new File("src/student.xml"));
Element rootElement=dom.getRootElement();
//添加元素
Element stuEle=rootElement.addElement("student");
stuEle.addAttribute("idcard", stu.getIdcard());
stuEle.addAttribute("examid", stu.getExamid());
//添加子节点
stuEle.addElement("name").setText(stu.getName());
stuEle.addElement("location").setText(stu.getLocation());
stuEle.addElement("score").setText(stu.getScore());
//更新XML (保存信息)
OutputFormat of=OutputFormat.createPrettyPrint();//格式化器
of.setEncoding("gb2312");
XMLWriter xw=new XMLWriter(new FileWriter("src/student.xml"),of);
xw.write(dom);
xw.close(); flay=true;
} catch (Exception e) {
e.printStackTrace();
} return flay; } @Override
public boolean removeStudent(String name) {
boolean flay=false;
Document dom=null;
//1.创建解析器
SAXReader reader = new SAXReader();
//2.解析文档 try {
dom = reader.read(new File("src/student.xml"));
Element root=dom.getRootElement(); //获取根节点
List<Element> list=root.elements("student"); //选择要对哪个元素进行操作
for (Element e : list) {
Element nameEle=e.element("name");
if(nameEle.getText().equals(name)){
root.remove(e);
flay=true;
} }
//更新
OutputFormat of=OutputFormat.createPrettyPrint();//格式化器
of.setEncoding("gb2312");
XMLWriter xw=new XMLWriter(new FileWriter("src/student.xml"),of);
xw.write(dom);
xw.close(); } catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return flay;
} @Override
public Student caxunStudent(String examid) {
Student s=null;
Document dom=null;
//1.创建解析器
SAXReader reader = new SAXReader();
//2.解析文档
try {
dom = reader.read(new File("src/student.xml"));
Element root=dom.getRootElement(); //获取根节点
List<Element> list=root.elements("student");
Element ele=null;
for (Element e : list) {
String str2=e.attribute("examid").getText();
//String str2=e.attributeValue("examid"); 与上一条意思一样
if(str2.equals(examid)){
ele=e;
break;
}
}
if(ele!=null){
String name=ele.elementText("name");
String location=ele.element("location").getText();
String score=ele.elementText("score");
String idcard=ele.attributeValue("idcard");
String examid1=ele.attributeValue("examid");
s=new Student(name, examid1, idcard, location, score);
}
} catch (DocumentException e) { e.printStackTrace();
}
return s;
} }

最后则是测试类及对添加等方法的测试

 /**
* 测试
* 2017-5-3
*下午2:40:47
*/
public class StudentBiz {
//static Student stu=new Student();
static Scanner input=new Scanner(System.in);
static AddStuimpl asi=new AddStuimpl();
public static void main(String[] args) {
System.out.println("添加学生(1)\t删除学生(2)\t查询成绩(3)\t退出(4)");
do{
System.out.print("请选择有效序号: ");
int a=input.nextInt();
switch(a){
case 1:
System.out.println("请输入学生的姓名");
String name=input.next();
System.out.println("请输入学生的准考证号");
String examid=input.next();
System.out.println("请输入学生的身份证号");
String idcard=input.next();
System.out.println("请输入学生的所在地");
String location=input.next();
System.out.println("请输入学生的成绩");
String score=input.next();
Student stu=new Student(name, examid, idcard, location, score); boolean bool=asi.addStudent(stu);
if(bool){
System.out.println("学生添加成功");
}else{
System.out.println("添加失败");
} continue;
case 2:
System.out.println("请输入要删除的学生姓名");
String removeName=input.next();
Student stu2=new Student();
//stu2.setName(removeName); RemoveStuImpl re=new RemoveStuImpl();
//boolean bool2=re.removeStu(stu2);
boolean bool2=asi.removeStudent(removeName);
if(bool2){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
continue;
case 3:
System.out.println("请输入学生考号");
String examid1=input.next();
Student stu3=asi.caxunStudent(examid1);
if(stu3!=null){
System.out.println("学生的身份证是"+stu3.getIdcard()+",准考证"+stu3.getExamid()+
",姓名"+stu3.getName()+",分数是: "+stu3.getScore()+",地址"+stu3.getLocation());
}else{
System.out.println("查无此人");
}
continue;
case 4:
System.out.println("系统退出");
break;
}
break;
}while(true); }
}

本文没有使用工具类,后续可自行添加!!!!!

DOM4J案例详解(添加 ,查询 ,删除 ,保存)的更多相关文章

  1. ​ 用一个开发案例详解Oracle临时表

    ​ 用一个开发案例详解Oracle临时表 2016-11-14 bisal ITPUB  一.开发需求  最近有一个开发需求,大致需要先使用主表,或主表和几张子表关联查询出ID(主键)及一些主表字段 ...

  2. linux route命令的使用详解 添加永久静态路由 tracert traceroute

    linux route命令的使用详解 添加永久静态路由  tracert  traceroute route -n    Linuxroute  print  Windows traceroute  ...

  3. 第7.24节 Python案例详解:使用property函数定义属性简化属性访问代码实现

    第7.24节 Python案例详解:使用property函数定义属性简化属性访问代码实现 一.    案例说明 本节将通过一个案例介绍怎么使用property定义快捷的属性访问.案例中使用Rectan ...

  4. jQuery基础入门+购物车案例详解

    jQuery是一个快速.简洁的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是"write Less,Do More",即倡导写更少的代码,做更多 ...

  5. spring的IOC,DI及案例详解

    一:spring的基本特征 Spring是一个非常活跃的开源框架:它是一个基于Core来架构多层JavaEE系统的框架,它的主要目的是简化企业开发.Spring以一种非侵入式的方式来管理你的代码,Sp ...

  6. 深入浅出 spring-data-elasticsearch - 基本案例详解(三

    『  风云说:能分享自己职位的知识的领导是个好领导. 』运行环境:JDK 7 或 8,Maven 3.0+技术栈:SpringBoot 1.5+, Spring Data Elasticsearch ...

  7. MySQL慢查询(二) - pt-query-digest详解慢查询日志 pt-query-digest 慢日志分析

    随笔 - 66 文章 - 0 评论 - 19 MySQL慢查询(二) - pt-query-digest详解慢查询日志 一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它 ...

  8. spss进行判别分析步骤_spss判别分析结果解释_spss判别分析案例详解

    spss进行判别分析步骤_spss判别分析结果解释_spss判别分析案例详解 1.Discriminant Analysis判别分析主对话框 如图 1-1 所示 图 1-1 Discriminant ...

  9. 第7.27节 Python案例详解: @property装饰器定义属性访问方法getter、setter、deleter

    上节详细介绍了利用@property装饰器定义属性的语法,本节通过具体案例来进一步说明. 一.    案例说明 本节的案例是定义Rectangle(长方形)类,为了说明问题,除构造函数外,其他方法都只 ...

随机推荐

  1. UIImageView帧动画相关属性和方法

    @property(nonatomic,copy) NSArray *animationImages; 需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片) @propert ...

  2. 深入理解Spring中bean的生命周期

    [Spring中bean的生命周期] bean的生命周期 1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期: (1).生命周期图: (2).具体事例 ...

  3. java做单用户的多重并发会话数限制

    判定条件很简单,就是在同一时刻,同一帐号仅在一个终端上可正常操作. 我这里用简单的key,value进行判定,将用户存储在map里面,新登录用户登陆进系统后,判断map里是否存在当前用户,若存在就删除 ...

  4. JS+CSS实现的下拉刷新/上拉加载插件

    闲来无事,写了一个当下比较常见的下拉刷新/上拉加载的jquery插件,代码记录在这里,有兴趣将代码写成插件与npm包可以留言. 体验地址:http://owenliang.github.io/pull ...

  5. iOS开发--Runtime的简单使用之关联对象

    一.Runtime关联对象的方法简介: 在<objc/runtime.h>中,有三个关联的方法,分别是: objc_setAssociatedObject objc_getAssociat ...

  6. [SinGuLaRiTy] 树形存储结构阶段性测试

    [SinGuLaRiTy-1011] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. G2019级信息奥赛专项训练 题目 程序名 时间 内存 ...

  7. VSCode里的一些有用的插件

    1.EaseServer 作用:开启本地 http server,然后在browser里打开html: 在调试页面的时候,打开页面比较方便,不需要先开启server,再打开html 2.Debugge ...

  8. mysql 分析第一步

    分析mysql 慢的原因    思路 通过脚本观察 status -->看是否会出现周期性波动 一般由访高峰或缓存崩溃引起   加缓存更改 缓存失效策略 使失效时间分散 或夜间定时失效 --&g ...

  9. TCP/IP笔记(五)IP协议相关技术

    IP旨在让最终目标主机收到数据包,但是在这一过程中仅仅有IP时无法实现通信的.必须还要又能够解析主机名称和MACdivide功能,以技术包在发送过程中异常情况处理的功能. 这篇主要介绍下DNS.ARP ...

  10. python_原始_web框架

    创:10_4_2017 修: 什么是web框架? -- 本质上是socket,用户请求来,业务逻辑处理,返回处理结果 -- 包含socket或者不包含socket的框架 什么是wsgi? -- web ...