科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道JAVA使用XML作为持久存储介质实现方法 (2)

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

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

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

作者:dxaw 来源:赛迪网 2007年11月6日

关键字: 存储介质 XML java

  • 评论
  • 分享微博
  • 分享邮件

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; 
} 
}
查看本文来源
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章