分享
 
 
 

JAVA使用XML作为持久存储介质实现方法

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

对一些需要将数据持久化的小型程序中,传统的关系型数据库显得庞大而不实用,OO数据库有一个学习曲线的问题,而使用XML是一种较好的选择.本文将就设计一个合理的XML持久化的解决方案进行探讨.

使用XML作为持久层解决方案的,它的基本功能要有:

1.对象的CRUD功能(本例中基本对象是Member类).

2.保证线程安全,对桌面程序和Web程序都一样适用.

3.有缓存,在存储介质即XML文件忽然丢失的情况下还能有效工作.

本例采用了MemberService和MemberPersistence两个类来协作完成这些功能.

MemberService是业务层和持久层之间的桥梁,用于对Member对象的CRUD操作,内置一个hashtable来缓存Member对象,即使文件忽然丢失,缓存中的数据也不会被影响.它内置一个MemberPersistence成员来完成与持久介质的交互.

实现添加,删除,更新的三个函数add(),delete(),update()都用lockObj实现了同步,这样就无需担心线程安全问题.其它函数对members成员没有修改,故不需要实现同步.

try{

memberPersistence.add(member);

members.put(member.getId(), member);

return true;

}

catch(XmlFileWriteException ex){

System.out.println("Member:" + member.getId() + " add error!");

return false;

}

当MemberPersistence添加对象成功后,这个对象才会被添加到members中,这样保证了缓存和实际数据的同步;假如颠倒一下顺序,那末MemberPersistence添加对象不成功时,出现XmlFileWriteException异常,这是还需要在catch中删除刚才添加的对象,这样做人为加大了程序的复杂度,不如上面的做法简单高效.

关于查询函数的做法不明白的请见 http://www.blogJava.net/sitinspring/archive/2007/06/05/122119.Html 中形式三.

下面是MemberService类的全部代码:

package com.sitinpsring.service;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Hashtable;

import java.util.Iterator;

import java.util.List;

import com.sitinpsring.domain.Member;

import com.sitinpsring.domain.MemberFilter;

import com.sitinpsring.exception.XmlFileReadException;

import com.sitinpsring.exception.XmlFileWriteException;

import com.sitinpsring.persistence.MemberPersistence;

public class MemberService {

private static Hashtable<String, Member> members;

private static MemberPersistence memberPersistence;

private static final Object lockObj = new Object();

static {

try {

memberPersistence = new MemberPersistence("member.xml");

members = memberPersistence.loadMemberFromFile();

} catch (XmlFileReadException ex) {

System.out.println("Can’t read the file:member.xml");

}catch (XmlFileWriteException ex) {

System.out.println("Can’t write to the file:member.xml");

}

}

public MemberService() {

}

public boolean hasMember(String id) {

return members.containsKey(id);

}

public boolean hasMember(Member member) {

return hasMember(member.getId());

}

public boolean add(Member member) {

if (hasMember(member)) {

System.out.println("Member:" + member.getId() + " has been exist!");

return false;

} else {

synchronized (lockObj) {

try{

memberPersistence.add(member);

members.put(member.getId(), member);

return true;

}

catch(XmlFileWriteException ex){

System.out.println("Member:" + member.getId() + " add error!");

return false;

}

}

}

}

public boolean update(Member member) {

if (hasMember(member)) {

synchronized (lockObj) {

try{

memberPersistence.update(member);

Member oldMember = members.get(member.getId());

oldMember.setName(member.getName());

oldMember.setAge(member.getAge());

return true;

}

catch(XmlFileWriteException ex){

System.out.println("Member:" + member.getId() + " upate error!");

return false;

}

}

} else {

System.out.println("Member:" + member.getId()

+ " can’t been found!");

return false;

}

}

public boolean saveOrUpdate(Member member) {

if (hasMember(member)) {

return update(member);

} else {

return add(member);

}

}

public boolean delete(Member member) {

if (hasMember(member)) {

synchronized (lockObj) {

try{

memberPersistence.delete(member.getId());

members.remove(member.getId());

return true;

}catch(XmlFileWriteException ex){

System.out.println("Member:" + member.getId() + " delete error!");

return false;

}

}

} else {

System.out.println("Member:" + member.getId()

+ " can’t been found!");

return false;

}

}

@SuppressWarnings("unchecked")

public List<Member> search(MemberFilter memberFilter) {

ArrayList<Member> retval = new ArrayList<Member>();

for (Iterator it = members.keySet().iterator(); it.hasNext();) {

String key = (String) it.next();

Member member = members.get(key);

if (memberFilter.accept(member)) {

retval.add(member);

}

}

Collections.sort(retval);

return retval;

}

public List<Member> getAll() {

MemberFilter filter = new MemberFilter() {

public boolean accept(Member member) {

return true;

}

};

return search(filter);

}

public Member getMemberById(String id) {

for (Iterator it = members.keySet().iterator(); it.hasNext();) {

String key = (String) it.next();

Member member = members.get(key);

if (member.getId().equals(id)) {

return member;

}

}

return null;

}

}

MemberPersistence类是与XML文件打交道的类,通过它,数据才能真正存入持久介质-XML文件.它的函数都很好理解.这些函数工作时实际只会引发两种异常--读XML文件异常(一般由多个根节点导致)和写XML文件异常,会触发这些异常的函数都应该对他们进行捕捉和抛出处理.

package com.sitinpsring.persistence;

import java.io.File;

import java.io.FileWriter;

import java.util.Hashtable;

import java.util.Iterator;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

import com.sitinpsring.domain.Member;

import com.sitinpsring.exception.XmlFileReadException;

import com.sitinpsring.exception.XmlFileWriteException;

public class MemberPersistence {

private String xmlFilePos;

private Document document;

public MemberPersistence(String xmlFilePos)

throws XmlFileReadException,XmlFileWriteException{

this.xmlFilePos = xmlFilePos;

if (isFileExist(this.xmlFilePos) == false) {

// Create document when file not exist

createDocument();

return;

} else {

// Get Docunent when file exist

SAXReader reader = new SAXReader();

try {

document = reader.read(new File(this.xmlFilePos));

} catch (Exception ex) {

throw new XmlFileReadException(ex.getMessage());

}

}

}

private void createDocument() throws XmlFileWriteException{

String text = "<members></members>";

try {

document = DocumentHelper.parseText(text);

writeDocumentToFile();

} catch (XmlFileWriteException ex) {

throw ex;

}catch (Exception ex) {

ex.printStackTrace();

}

}

private void writeDocumentToFile() throws XmlFileWriteException{

try {

XMLWriter writer = new XMLWriter(new FileWriter(this.xmlFilePos));

writer.write(document);

writer.close();

} catch (Exception ex) {

throw new XmlFileWriteException(ex.getMessage());

}

}

public Hashtable<String, Member> loadMemberFromFile() {

Hashtable<String, Member> retval=new Hashtable<String, Member>();

List nodes = document.getRootElement().elements("member");

for (Iterator it = nodes.iterator(); it.hasNext();) {

Element elm = (Element) it.next();

Member member = new Member(elm.attributeValue("id"),elm.elementText("name"),

Integer.parseInt(elm.elementText("age")));

retval.put(member.getId(), member);

}

return retval;

}

public boolean add(Member member) throws XmlFileWriteException{

try {

Element rootElm = document.getRootElement();

Element newMemberElm = rootElm.addElement("member");

newMemberElm.addAttribute("id", member.getId());

Element nameElm=newMemberElm.addElement("name");

nameElm.setText(member.getName());

Element ageElm=newMemberElm.addElement("age");

ageElm.setText(String.valueOf(member.getAge()));

writeDocumentToFile();

return true;

} catch (XmlFileWriteException ex) {

throw ex;

}

}

public boolean update(Member member) throws XmlFileWriteException{

try {

Element rootElm = document.getRootElement();

List nodes = rootElm.elements("member");

for (Iterator it = nodes.iterator(); it.hasNext();) {

Element elm = (Element) it.next();

if(elm.attributeValue("id").equals(member.getId())){

elm.element("name").setText(member.getName());

elm.element("age").setText(String.valueOf(member.getAge()));

break;

}

}

writeDocumentToFile();

return false;

} catch (XmlFileWriteException ex) {

throw ex;

}

}

public boolean delete(String id) throws XmlFileWriteException{

try {

Element rootElm = document.getRootElement();

List nodes = rootElm.elements("member");

for (Iterator it = nodes.iterator(); it.hasNext();) {

Element elm = (Element) it.next();

Member member = new Member(elm.attributeValue("id"),elm.elementText("name"),

Integer.parseInt(elm.elementText("age")));

if(member.getId().equals(id)){

rootElm.remove(elm);

break;

}

}

writeDocumentToFile();

return false;

} catch (XmlFileWriteException ex) {

throw ex;

}

}

public static boolean isFileExist(String filePath) {

boolean exists = (new File(filePath)).exists();

return exists;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有