使用dom4j技术对xml文件的基本操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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文件的基本操作的主要内容,如果未能解决你的问题,请参考以下文章

java代码用dom4j解析xml文件的简单操作

1.使用dom4j解析XML文件

XML编程总结——使用dom4j方式操作xml

xml学习

java对XML文件的相关操作(dom4j)

成都校区Dom4J解析XML的范例浅析