博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用dom4j技术对xml文件的基本操作
阅读量:4588 次
发布时间:2019-06-09

本文共 6819 字,大约阅读时间需要 22 分钟。

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 List
url=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();			List
elements = 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); }}

  

转载于:https://www.cnblogs.com/maybo/p/5182552.html

你可能感兴趣的文章
oracle创建临时表没有权限
查看>>
76.数塔问题
查看>>
PHP 透明水印生成代码
查看>>
我就是学习
查看>>
Hibernate的缓存
查看>>
(十五)Struts2 本地化/国际化(i18n)
查看>>
利用Qt Phonon框架制作音视频播放器
查看>>
Bundle savedInstanceState的作用
查看>>
打印图片,自动调整宽高
查看>>
对类使用dir()
查看>>
【13】淘宝sdk——入门实战之header.php制作(一)
查看>>
安装SoapUI Pro
查看>>
杜教BM模板
查看>>
Makefile经典教程(掌握这些足够)
查看>>
自己成功的编写的将数据从excel导入到access中
查看>>
【Leetcode】【Easy】Compare Version Numbers
查看>>
014 链表中倒数第k个结点
查看>>
Python的pip安装Django
查看>>
第一冲刺阶段——站立会议第二天4月19日
查看>>
hdu-----(2807)The Shortest Path(矩阵+Floyd)
查看>>