使用dom4j技术对xml文件的基本操作
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文件的基本操作的更多相关文章
- dom4j加载xml文件
## dom4j加载xml文件 ``` // 1. 加载xml文件 InputStream is = MyTest.class.getResourceAsStream("user.xml&q ...
- Java中使用DOM4J来生成xml文件和解析xml文件
一.前言 现在有不少需求,是需要我们解析xml文件中的数据,然后导入到数据库中,当然解析xml文件也有好多种方法,小编觉得还是DOM4J用的最多最广泛也最好理解的吧.小编也是最近需求里遇到了,就来整理 ...
- # java对xml文件的基本操作
下面是简单的总结三种常用的java对xml文件的操作 1. dom方式对xml进行操作,这种操作原理是将整个xml文档读入内存总,在内存中进行操作,当xml文档非常庞大的时候就会出现内存溢出的异常,这 ...
- XML:使用DOM技术解析xML文件中的城市,实现select级联选择
中国的城市xml格式:cities.xml <?xml version="1.0" encoding="utf-8"?> <china> ...
- dom4j如何解析XML文件
最近在 一些对xml文件的操作,下面简单写一个dom4j解析xml文件并将其封装到一个javabean中的例子,只是具有针对性的,不是通用的,仅供参考哦~~ 首先说:dom4j是一个java的XML ...
- dom4j的读写xml文件,读写xml字符串
百度了一些博客,大同小异,在选取jar包工具的时候大概看了下,大抵是jdom原始,dom4j优秀.于是做了些练习. 参考:http://www.cnblogs.com/mengdd/archive/2 ...
- DOM4J方式解析XML文件
dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory dom4j是一个简单的开源库,用于处理XML. X ...
- dom4j: 生成的XML文件根节点 xmlns="" 的问题
使用dom4j写入XML文件时,写入完毕后发现root element中没有 xmlns,也即是没有命名空间. 正确的写法如下: Document document = DocumentHelper. ...
- simpleXML技术解析xml文件(php)
1.simpleXML的核心思想:以面向对象的方法来操作xml文件 此技术可以将xml文件的所有元素都转成对象.会返回一个对象数组,再用foreach遍历,即可得到元素的名称,内容,和属性值. tes ...
随机推荐
- Flex 三态复选框
在周末挤出了一点时间,写了一个三态复选框的组件,单独使用没有价值,不过集成到树之中可以很好的实现三态树,今天上午便把三态树组件也完成了,Flex自定义组件基本无所不能,此组件基于最新的Flex4.6( ...
- 【练习】sqlnet.ora
在SQLNET.ora文件中设置以下参数可以实现IP访问限制: $ pwd/u01/app/oracle/product/10.2.0/db_1/network/admin$ vi sqlnet.or ...
- 不能读取文件“itunes.library.itl”因为它是由更高级别的itunes所创建的
转自:https://zhidao.baidu.com/question/80796363.html 是因为你安装过高版本的后又装你版本的itunes. 你在电脑上搜索所有硬盘上的itunes lib ...
- USACO Section 3.2 01串 Stringsobits
题目背景 考虑排好序的N(N<=31)位二进制数. 题目描述 他们是排列好的,而且包含所有长度为N且这个二进制数中1的位数的个数小于等于L(L<=N)的数. 你的任务是输出第i(1< ...
- biji001
指针对变量使对指向变量的指针&运算符产生,对指针使用*运算符则可以返回到原始变量只要p指向i,那么*p就是i的别名*p不仅仅拥有和i同样的值,而且对*p的改变i的值*p左值,对它赋值合法*p ...
- Oracle多线程并行使用、关联与指定索引执行
nologging AS SELECT /*+parallel(4) leading(s a) use_hash(A) index(s IDX_CS_SERVICE_RECORD_MD2_04) */ ...
- javascript之值传递与引用传递
在分析这个问题之前,我们需了解什么是按值传递(call by value),什么是按引用传递(call by reference).在计算机科学里,这个部分叫求值策略(Evaluation Strat ...
- ie使用firebug
在网页插入以下代码即可. <script type="text/javascript" src="http://getfirebug.com/releases/li ...
- Winform开发常用控件之Checkbox和CheckedListBox
Winform的开发基本都是基于控件事件的,也就是事件驱动型的. 多选框的放置和值的获取有很多种,这里介绍几个简单常用的方法 1.直接放置Checkbox,并获取Checkbox的值 上图 做法也非常 ...
- Ubuntu12.04卡死的解决方案
刚开始安装的时候用着还行,不过后来发现用了一会总是会出现卡死的状况 后来看了下ubuntu12.04的内核是3.2,后来把内核升级到3.5发现这种情况不会出现了. 查看内核以及升级内核 uname - ...