最近使用 dom4j包来操作了xml 文件,具体有 search, add, modi,del 操作,下面将一个 javaBean 展示出来给大伙看看。
package com.jim.beans;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class DepartmentBean {
private String ID; //主键,没有任何意义
private String bh; //部门编号
private String name; //部门名称
private String tel; //部门电话
private String address; //部门地址
private File file; //要读取的 xml文件
public DepartmentBean() {
}
public DepartmentBean(File file) {
this.file = file;
}
public DepartmentBean(String id, String bh, String name, String tel, String address) {
ID = id;
this.bh = bh;
this.name = name;
this.tel = tel;
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBh() {
return bh;
}
public void setBh(String bh) {
this.bh = bh;
}
public String getID() {
return ID;
}
public void setID(String id) {
ID = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
//查询若干个部门 支持模糊查询
public ArrayList search(String attrName,String attrValue) throws DocumentException {
ArrayList al = new ArrayList();
List list = null;
//File f = new File("d:\\department.xml"); //把要解析的xml文件包装成File类型
SAXReader saxReader = new SAXReader(); //加载XML文档
saxReader.setEncoding("UTF-8");
Document document = saxReader.read(this.file);
/* 采用了 dom4j 支持 xPath 这个用法 */
if((attrName == null || attrName.trim().equals(""))
||(attrValue == null || attrValue.trim().equals(""))) {
list = document.selectNodes("/departments/department");
}else {
//[注]: 下面这句采用的是 精确查询 等效于 某个属性=值
//list = document.selectNodes("/departments/department[@"+attrName+"='"+attrValue+"']");
//[注]: 下面这句采用的是 模糊查询 等效于 某个属性 like %值%
list = document.selectNodes("/departments/department[contains(@"+attrName+",'" + attrValue + "')]");
}
Iterator iterator = list.iterator();
DepartmentBean departmentBean = null;
while(iterator.hasNext()) {
Element element = (Element)iterator.next();
departmentBean = new DepartmentBean();
departmentBean.setID(element.attributeValue("id"));
departmentBean.setBh(element.attributeValue("bh"));
departmentBean.setName(element.attributeValue("name"));
departmentBean.setTel(element.attributeValue("tel"));
departmentBean.setAddress(element.attributeValue("address"));
al.add(departmentBean);
}
return al;
}
//查询某个部门
public DepartmentBean getDepartment(String attrName,String attrValue) throws DocumentException {
ArrayList al = search(attrName,attrValue);
if(al != null && al.size() > 0) {
return (DepartmentBean)al.get(0);
}else {
return null;
}
}
//添加部门
public void add(DepartmentBean newDepartmentBean) throws DocumentException, IOException {
Document document = DocumentHelper.createDocument();
//创建根节点,并返回要节点
Element departmentsElement = document.addElement("departments");
departmentsElement.addComment("所有的部门信息");
//向根节点departments中加入department子节点
Element departmentElement = null;
departmentElement = departmentsElement.addElement("department");
departmentElement.addAttribute("id",newDepartmentBean.getID());
departmentElement.addAttribute("bh",newDepartmentBean.getBh());
departmentElement.addAttribute("name",newDepartmentBean.getName());
departmentElement.addAttribute("tel",newDepartmentBean.getTel());
departmentElement.addAttribute("address",newDepartmentBean.getAddress());
//File f = new File("d:\\department.xml"); //把要解析的xml文件包装成File类型
SAXReader saxReader = new SAXReader(); //加载XML文档
saxReader.setEncoding("UTF-8");
Document documentRead = saxReader.read(this.file);
List list = documentRead.selectNodes("/departments/department");
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
departmentElement = departmentsElement.addElement("department");
Element element = (Element)iterator.next();
Attribute attrID = element.attribute("id");
departmentElement.addAttribute("id",attrID.getValue());
Attribute attrBh = element.attribute("bh");
departmentElement.addAttribute("bh",attrBh.getValue());
Attribute attrName = element.attribute("name");
departmentElement.addAttribute("name",attrName.getValue());
Attribute attrTel = element.attribute("tel");
departmentElement.addAttribute("tel",attrTel.getValue());
Attribute attrAddress = element.attribute("address");
departmentElement.addAttribute("address",attrAddress.getValue());
}
//把修改后document写到原有的department.xml文件,以覆盖原有的department.xml文件
XMLWriter output = new XMLWriter(new OutputStreamWriter(new FileOutputStream(this.file),"UTF-8"));
output.write(document);
output.close();
}
//编辑部门
public void edit(String attrName,String attrValue,DepartmentBean newDepartmentBean) throws DocumentException, IOException {
//File f = new File("d:\\department.xml"); //把要解析的xml文件包装成File类型
SAXReader saxReader = new SAXReader(); //加载XML文档
saxReader.setEncoding("UTF-8");
Document document = saxReader.read(this.file);
List list = null;
Iterator iterator = null;
if((attrName == null || attrName.trim().equals(""))
||(attrValue == null || attrValue.trim().equals(""))) { //如果设置的修改条件为 null ,则什么也不做
return ;
}else {
list = document.selectNodes("/departments/department[@"+attrName+"='"+attrValue+"']");
}
if(list == null || list.size() == 0) { //如果设置的修改条件没有符合的记录,则什么也不做
return ;
}else {
iterator = list.iterator();
while(iterator.hasNext()) {
Element e = (Element) iterator.next();
Attribute attrB = e.attribute("bh");
attrB.setValue(newDepartmentBean.getBh());
Attribute attrN = e.attribute("name");
attrN.setValue(newDepartmentBean.getName());
Attribute attrT = e.attribute("tel");
attrT.setValue(newDepartmentBean.getTel());
Attribute attrA = e.attribute("address");
attrA.setValue(newDepartmentBean.getAddress());
}
}
//把修改后document写到原有的department.xml文件,以覆盖原有的department.xml文件
XMLWriter output = new XMLWriter(new OutputStreamWriter(new FileOutputStream(this.file),"UTF-8"));
output.write(document);
output.close();
}
//删除部门
public void del(String attrName,String attrValue) throws DocumentException, IOException {
//File f = new File("d:\\department.xml"); //把要解析的xml文件包装成File类型
SAXReader saxReader = new SAXReader(); //加载XML文档
saxReader.setEncoding("UTF-8");
Document documentRead = saxReader.read(this.file);
Document documentWrite = null;
List list = null;
if((attrName == null || attrName.trim().equals(""))
||(attrValue == null || attrValue.trim().equals(""))) { //如果设置的修改条件为 null ,则什么也不做
return ;
}else {
list = documentRead.selectNodes("/departments/department[@"+attrName+"!='"+attrValue+"']");
}
documentWrite = DocumentHelper.createDocument();
Element departmentsElement = documentWrite.addElement("departments");
departmentsElement.addComment("所有的部门信息");
if(list == null || list.size() == 0) { //如果是全删除了,则什么也不做
//把修改后document写到原有的department.xml文件,以覆盖原有的department.xml文件
XMLWriter output = new XMLWriter(new OutputStreamWriter(new FileOutputStream(this.file),"UTF-8"));
output.write(documentWrite);
output.close();
return ;
}else {
//向根节点departments中加入department子节点
Element departmentElement = null;
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
departmentElement = departmentsElement.addElement("department");
Element element = (Element)iterator.next();
Attribute attrID = element.attribute("id");
departmentElement.addAttribute("id",attrID.getValue());
Attribute attrBh = element.attribute("bh");
departmentElement.addAttribute("bh",attrBh.getValue());
Attribute attrName2 = element.attribute("name");
departmentElement.addAttribute("name",attrName2.getValue());
Attribute attrTel = element.attribute("tel");
departmentElement.addAttribute("tel",attrTel.getValue());
Attribute attrAddress = element.attribute("address");
departmentElement.addAttribute("address",attrAddress.getValue());
}
//把修改后document写到原有的department.xml文件,以覆盖原有的department.xml文件
XMLWriter output = new XMLWriter(new OutputStreamWriter(new FileOutputStream(this.file),"UTF-8"));
output.write(documentWrite);
output.close();
}
}
//得到 xml 文件中最大的主键值 + 1
public int getMaxID() throws DocumentException {
SAXReader saxReader = new SAXReader(); //加载XML文档
saxReader.setEncoding("UTF-8");
Document document = saxReader.read(this.file);
List list = document.selectNodes("/departments/department");
Iterator iterator = null;
int max = 0;
if(list == null || list.size() == 0) {
max = 0;
}else {
iterator = list.iterator();
while(iterator.hasNext()) {
Element element = (Element)iterator.next();
String maxStr = element.attributeValue("id");
int maxInt = Integer.parseInt(maxStr);
if(maxInt > max) {
max = maxInt;
}
}
}
return max + 1;
}
public static void main(String[] args) throws DocumentException, IOException {
File f = new File("d:\\department.xml");
DepartmentBean db = new DepartmentBean(f);
System.out.println(db.getMaxID());
//大家可以在这里写一些调用上面的 search ,add , del , edit 方法代码来测试测试,department.xml 文件在附件中已上传了此外还要要到两个特殊的包一个名叫jaxen-1.1.1.jar,还有一个叫dom4j-1.6.1.jar大家可以自己下载
}
}