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. docker-tomcat-nginx 反向代理和负载均衡

    1.部署tomcat镜像 下载官方的tomcat镜像. -jre7 启动docker容器,2个实例,分别映射不同的端口号, ~/work/sample-webapps/[v1.0|v2.0]/下面存放 ...

  2. COM学习(三)——数据类型

    上回书介绍了GUID.CLSID.IID和接口的概念.本回的重点是介绍 COM 中的数据类型.咋还不介绍组件程序的设计步骤呀?咳......别着急,别着急!孔子曰:"饭要一口一口地吃&quo ...

  3. 安装office2013时弹出microsoft setup bootstrapper已停止工作,接着就不能安装了

    只是说一下自己的解决方法: 我的原因是注册表没有删除改进. 1.输入regedit 2.找到HKEY_LOCAL_MACHINE\software\microsoft\windows\CurrentV ...

  4. (笔记)angular选项卡变色

  5. java初始化深度剖析

    当用一个类来创建对象的时候,JVM会帮我们执行下面的6个步骤. 1.在磁盘上找到对应内的class文件: 2.加载class文件到内存,初始化所有静态域(成员变量和方法): 3.如果使用new cla ...

  6. 手机连接wifi自动弹窗的原理及其实现方案

    一.手机连上wifi后会自动弹窗的原理 生活中,有很多需要认证的路由器,手机连接wifi热点后会自动弹出一个网页,让用户输入账号和密码,比如星巴克,肯地基,麦当劳,甚至是火车站和机场的候车室.其实这是 ...

  7. 查看cics 运行状态

     查看cics 运行状态cicscp -v status all 

  8. CentOS6.5安装readline时报错:/usr/bin/ld : cannot find -lncurses

    CentOS6.5安装readline时报错:/usr/bin/ld : cannot find -lncurses 解决方法: 安装ncurses-devel,输入命令: #yum install ...

  9. Python开发的3种命令执行方法

    在python开发中,我们常常需要执行命令,修改相关信息.那对于初学者来说,python中如何执行命令呢?今天,小编就为大家分享3种python命令执行的方法. 1. 使用os.system(&quo ...

  10. 利用Jquery处理跨域请求

    在项目制作过程中,可能会用到ajax来提高用户体验,这里终于研究出来,利用jquery来进行跨域请求,在用$.getJSON这个方法时,前台页面中需这样写 $.getJSON(“需要提交处理的url? ...