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<String>url=new ArrayList<String>();
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<String> getUrl() {
return url;
}
public void setUrl(List<String> url) {
this.url = url;
}
@Override
public 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<Element> 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<Element> 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<Notice> finds() throws SAXException, DocumentException {
Document document = documentFromPath(path);
Element root = document.getRootElement();
List<Element> elements = root.elements();
List<Notice> notices = new ArrayList<Notice>();
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<String> urls = new ArrayList<String>();
List<Element> urlsEl = e.elements("url");
for (Element u : urlsEl) {
urls.add(u.getText());
notice.setUrl(urls);
}
notices.add(notice);
}
return notices;
} @Override
public List<Notice> finds(int index, int offset) throws SAXException,
DocumentException {
Document document = documentFromPath(path);
Element root = document.getRootElement();
List<Element> elements = root.elements();
if (offset < elements.size()) {
elements = elements.subList(index, offset);
}
List<Notice> notices = new ArrayList<Notice>();
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<String> urls = new ArrayList<String>();
List<Element> 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<Element> elements = root.elements();
return elements.size();
} @Override
public synchronized void clearEnd() throws IOException, SAXException, DocumentException {
Document document = documentFromPath(path);
Element root = document.getRootElement();
List<Element> elements = root.elements();
elements.remove(elements.size()-1);
write(document);
} }

  

使用dom4j技术对xml文件的基本操作的更多相关文章

  1. dom4j加载xml文件

    ## dom4j加载xml文件 ``` // 1. 加载xml文件 InputStream is = MyTest.class.getResourceAsStream("user.xml&q ...

  2. Java中使用DOM4J来生成xml文件和解析xml文件

    一.前言 现在有不少需求,是需要我们解析xml文件中的数据,然后导入到数据库中,当然解析xml文件也有好多种方法,小编觉得还是DOM4J用的最多最广泛也最好理解的吧.小编也是最近需求里遇到了,就来整理 ...

  3. # java对xml文件的基本操作

    下面是简单的总结三种常用的java对xml文件的操作 1. dom方式对xml进行操作,这种操作原理是将整个xml文档读入内存总,在内存中进行操作,当xml文档非常庞大的时候就会出现内存溢出的异常,这 ...

  4. XML:使用DOM技术解析xML文件中的城市,实现select级联选择

    中国的城市xml格式:cities.xml <?xml version="1.0" encoding="utf-8"?> <china> ...

  5. dom4j如何解析XML文件

    最近在 一些对xml文件的操作,下面简单写一个dom4j解析xml文件并将其封装到一个javabean中的例子,只是具有针对性的,不是通用的,仅供参考哦~~ 首先说:dom4j是一个java的XML ...

  6. dom4j的读写xml文件,读写xml字符串

    百度了一些博客,大同小异,在选取jar包工具的时候大概看了下,大抵是jdom原始,dom4j优秀.于是做了些练习. 参考:http://www.cnblogs.com/mengdd/archive/2 ...

  7. DOM4J方式解析XML文件

    dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory dom4j是一个简单的开源库,用于处理XML. X ...

  8. dom4j: 生成的XML文件根节点 xmlns="" 的问题

    使用dom4j写入XML文件时,写入完毕后发现root element中没有 xmlns,也即是没有命名空间. 正确的写法如下: Document document = DocumentHelper. ...

  9. simpleXML技术解析xml文件(php)

    1.simpleXML的核心思想:以面向对象的方法来操作xml文件 此技术可以将xml文件的所有元素都转成对象.会返回一个对象数组,再用foreach遍历,即可得到元素的名称,内容,和属性值. tes ...

随机推荐

  1. 个人收集(转载)CSS中 display:none和visibility:hidden的区别

    visibility和display两个属性都有隐藏元素的功能,display:none和visibility:hidden的区别,简单的总结一句话就是:visibility:hidden隐藏,但在浏 ...

  2. 慕课网-安卓工程师初养成-2-10 Java中的强制类型转换

    来源:http://www.imooc.com/code/1241 相信小伙伴们也发现了,尽管自动类型转换是很方便的,但并不能满足所有的编程需要. 例如,当程序中需要将 double 型变量的值赋给一 ...

  3. EXT学习之——Ext下拉框绑定以及级联写法

    /*******步骤有四个,缺一不可*********/ function () {xxxxxx = Ext.extend(construct, {InitControl: function () { ...

  4. 学习资料 数据查询语言DQL

    数据查询语言DQL介绍及其应用: 查询是SQL语言的核心,SQL语言只提供唯一一个用于数据库查询的语句,即SELECT语句.用于表达SQL查询的SELECT语句是功能最强也是最复杂的SQL语句,它提供 ...

  5. 华为OJ平台——24点游戏

    题目描述: 给出4个1-10的数字,通过加减乘除,得到数字为24就算胜利 输入: 4个1-10的数字.[数字允许重复,测试用例保证无异常数字]输出: true or false 思路:

  6. 二模08day2解题报告

    T1.引爆炸弹(bomb) N个炸弹构成一棵树,引爆一颗叶节点,会一直引爆到根节点.每颗炸弹有一个价值,求引爆k个炸弹的最大价值. 既然是一棵树,那么自然想到dp.所以先树形dp了一遍(由于可能出现多 ...

  7. Nodejs笔记(二)

    Nodejs事件 Node.js 所有的异步I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时分发一个事件, 一 ...

  8. C++: std::string 与 Unicode 结合

    一旦知道 TCHAR 和_T 是如何工作的,那么这个问题很简单.基本思想是 TCHAR 要么是char,要么是 wchar_t,这取决于_UNICODE 的值: // abridged from tc ...

  9. 2016-03-24:Windows内存泄露分析工具

    参考资料 100%正确的内存泄漏分析工具 ------ tMemMonitor (TMM)

  10. 用verilog模拟DDS产生正弦波信号

    前言: DDS:直接数字频率合成,正弦波0-2pi周期内,相位到幅度是一一对应的(这里我们使用放大后的整数幅度). 主要思路: 个人理解,FPGA不擅长直接做数字信号计算,那样太占用片上逻辑资源,所以 ...