1.pojo类:Notice
package com.green.notice.storage;import java.util.ArrayList;import java.util.List;public class Notice {private int id;private String title;private String content;private Listurl=new ArrayList ();private String createTime;private String uid;public void setUid(String uid) { this.uid = uid;}public String getUid() { return uid;}public void setId(int id) { this.id = id;}public int getId() { return id;}public void setCreateTime(String createTime) { this.createTime = createTime;}public String getCreateTime() { return createTime;}public String getTitle() { return title;}public void setTitle(String title) { this.title = title;}public String getContent() { return content;}public void setContent(String content) { this.content = content;}public List getUrl() { return url;}public void setUrl(List url) { this.url = url;}@Overridepublic String toString() { return "Notice [title=" + title + ", content=" + content + ", url=" + url + "]";}}
2 .解析类
package com.green.notice.storage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.swing.filechooser.FileView;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;import org.xml.sax.SAXException;/* * @author:maybo * @date:2016-1-5 * ��ͨ����Ϣ�Ľ��� */public class NoticeXmlParse implements IParseXml { private String path = ""; private String root = "notices"; /* * @param:xml�ļ�·�� */ public NoticeXmlParse(String path) { this.path = path; } @Override public synchronized void add(Notice notice) { try { File file=new File(path); if(!file.exists()) create(root); Document document = documentFromPath(path); Element root = document.getRootElement(); Listelements = root.elements(); Element element = DocumentHelper.createElement("notice"); element.addAttribute("id", elements.size()<=0?1+"":(Integer.valueOf( elements.get(0).attributeValue("id", null) )+1) + ""); element.addAttribute("title", notice.getTitle()); element.addAttribute("content", notice.getContent()); element.addAttribute("createTime", notice.getCreateTime()); for (String url : notice.getUrl()) { Element u = DocumentHelper.createElement("url"); u.setText(url); element.add(u); } elements.add(0, element); write(document); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public synchronized void delete(int id) { try { Document document = documentFromPath(path); Element root = document.getRootElement(); Iterator el = root.elementIterator("notice"); while (el.hasNext()) { if ((id + "").equals(el.next().attributeValue("id", null))) { el.remove(); } } write(document); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public List finds() throws SAXException, DocumentException { Document document = documentFromPath(path); Element root = document.getRootElement(); List elements = root.elements(); List notices = new ArrayList (); for (Element e : elements) { Notice notice = new Notice(); notice.setContent(e.attributeValue("content", null)); notice.setCreateTime(e.attributeValue("createTime", null)); notice.setId(Integer.valueOf(e.attributeValue("id", null))); notice.setTitle(e.attributeValue("title", null)); List urls = new ArrayList (); List urlsEl = e.elements("url"); for (Element u : urlsEl) { urls.add(u.getText()); notice.setUrl(urls); } notices.add(notice); } return notices; } @Override public List finds(int index, int offset) throws SAXException, DocumentException { Document document = documentFromPath(path); Element root = document.getRootElement(); List elements = root.elements(); if (offset < elements.size()) { elements = elements.subList(index, offset); } List notices = new ArrayList (); for (Element e : elements) { Notice notice = new Notice(); notice.setContent(e.attributeValue("content", null)); notice.setCreateTime(e.attributeValue("createTime", null)); notice.setId(Integer.valueOf(e.attributeValue("id", null))); notice.setTitle(e.attributeValue("title", null)); List urls = new ArrayList (); List urlsEl = e.elements("url"); for (Element u : urlsEl) { urls.add(u.getText()); notice.setUrl(urls); } notices.add(notice); } return notices; } private Document documentFromPath(String path) throws SAXException, DocumentException { SAXReader reader = new SAXReader(); Document document=null; if(new File(path).exists()){ document = reader.read(new File(path)); } return document; } @Override public void create(String root) throws DocumentException, IOException { // �Ű�����ĸ�ʽ OutputFormat format = OutputFormat.createPrettyPrint(); // ���ñ��� format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new OutputStreamWriter( new FileOutputStream(new File(path)), "UTF-8"), format); Document document = DocumentHelper.createDocument(); Element elementRoot = DocumentHelper.createElement(root); document.setRootElement(elementRoot); writer.write(document); // ����д�� writer.flush(); // �رղ��� writer.close(); } private void write(Document document) throws IOException { // �Ű�����ĸ�ʽ OutputFormat format = OutputFormat.createPrettyPrint(); // ���ñ��� format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new OutputStreamWriter( new FileOutputStream(new File(path)), "UTF-8"), format); writer.write(document); // ����д�� writer.flush(); // �رղ��� writer.close(); } @Override public int total() throws SAXException, DocumentException, IOException { File file=new File(path); if(!file.exists()) create(root); Document document = documentFromPath(path); Element root = document.getRootElement(); List elements = root.elements(); return elements.size(); } @Override public synchronized void clearEnd() throws IOException, SAXException, DocumentException { Document document = documentFromPath(path); Element root = document.getRootElement(); List elements = root.elements(); elements.remove(elements.size()-1); write(document); }}