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. 【练习】数据文件的更改:改名或改路径 users01.dbf-->users01_bak.dbf

    方法一:1.将数据文件的状态offline SQL> select file_name,tablespace_name from dba_data_files where file_name l ...

  2. Hot OS'15 summary

    My OS Ought to Know Me Better: In-app Behavioural Analytics as an OS Service   Earlence Fernandes, U ...

  3. C#中如何判断联系电话的合法性

    string tel = tb_tel.Text.Trim();//联系电话if (!string.IsNullOrEmpty(tb_tel.Text.Trim())){try{//num = Con ...

  4. MVC 百度地图的基本使用

    最近做的这个项目里面为了方便路线查询,将百度地图的插件加到了项目里,效果图如下: 下面我就把我的步骤贴出来: 第一步:进网站 http://developer.baidu.com/map/注册 第二步 ...

  5. [zt]Which are the 10 algorithms every computer science student must implement at least once in life?

    More important than algorithms(just problems #$!%), the techniques/concepts residing at the base of ...

  6. verilog中符号位的扩展问题

    以下内容转自 艾米电子 - 使用有符号数,Verilog(http://www.cnblogs.com/yuphone/archive/2010/12/12/1903647.html) Verilog ...

  7. SQLserver2008使用表达式递归查询(由父往子,由子往父)

    SQLserver2008使用表达式递归查询语句 --由父项递归下级 with cte(id,parentid,text) as (--父项 select id,parentid,text from ...

  8. Oracle笔记 十三、PL/SQL面向对象之package

    --将方法和过程用包定义 create or replace package pkg_emp as --输入员工编号查询出员工信息 procedure pro_findInfo( in_empno e ...

  9. grunt 快速入门

    Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本号的 No ...

  10. 带Left Join的SQL语句的执行顺序

    基础的SQL执行顺序 SQL语句执行的时候是有一定顺序的.理解这个顺序对SQL的使用和学习有很大的帮助. 1.from 先选择一个表,或者说源头,构成一个结果集. 2.where 然后用where对结 ...