程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JAVA使用XML作為持久存儲介質實現方法

JAVA使用XML作為持久存儲介質實現方法

編輯:關於JAVA

對一些需要將數據持久化的小型程序中,傳統的關系型數據庫顯得龐大而不實用,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;
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved