使用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 ...
随机推荐
- python中get、post数据
方法一:urllib2 参考:http://www.cnblogs.com/chenzehe/archive/2010/08/30/1812995.html post: #!/usr/bin/pyth ...
- 学习练习 使用Servlet实现用户注册功能
package com.hanqi.www; import java.io.IOException; import javax.servlet.ServletException; import jav ...
- 洛谷P1530 分数化小数 Fractions to Decimals
P1530 分数化小数 Fractions to Decimals 103通过 348提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目 ...
- PAT1075. PAT Judge
//终于A了,不难却觉着坑多的的题,注意-1的处理,感觉我是受memset置0的束缚了,可以把初试成绩置-1.就不用debug怎么久,注意对于-1的处理,不然漏洞百出 #include<cstd ...
- 【ILSpy反编译】C# 写的程序反编译查看是不是也太容易了点吧,太恐怖了。。。
最近由于要写一些界面的东西,写了几个月c#(之前一直做c/c++项目),发现c#写界面很方便,效果也不错,在这个过程中也听说c#程序可以很容易被反编译到,但一直也没时间去自己反编译去试着看看,心想就算 ...
- Eclipse Egit 安装
help->Install new software-> 在 work within 中输入以下网址 安装地址: http://download.eclipse.org/egit/upda ...
- Apache虚拟主机配置(多个域名访问多个目录)
Apache虚拟主机配置(多个域名访问多个目录) 为了方便管理虚拟主机,我决定使用一种方法,那就是修改httpd-vhosts.conf文件. 第一步首先要使扩展文件httpd-vhosts.conf ...
- systemctl
旧指令 新指令 使某服务自动启动 chkconfig --level 3 httpd on systemctl enable httpd.service 使某服务不自动启动 chkconfig - ...
- static 在多台下的特性
被static修饰的方法不具备多台的特性,因为这个时候,该方法已经不具备"后期绑定"的性质了,也就是说,基类的引用就算指向导出类,调用的static的方法还是用基类的. 如果要调用 ...
- js ajax乱码查看\u8fdb\u53e3
document.write('\u8fdb\u53e3') //在页面上看乱码转为中文 或在按F12 在console里查看 直接打'\u8fdb\u53e3'