JAVA使用XML作为持久存储介质实现方法 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-18
; 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 (XmlFileWriteExc |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于JAVA使用XML作为持久存储介质实现方法 - 编程入门网的所有评论