package zgw.com.javabean;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class OpXML {
public Document dom;
public int pageSize = 5;// 页面大小
public int resultCount = 0;// 总共记录条数
public int pageNum = 1;// 总共页面
public Element root = null;
public OpXML() {
SAXReader read = new SAXReader();
try {
dom = read.read(new File("c://leavemessage.xml"));
} catch (DocumentException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
root = dom.getRootElement();
}
/*
* 取得记录条数
*
*/
public int getResultCount() {
int a = 0;
for (Iterator it = root.elementIterator(); it.hasNext(); it.next()) {
a++;
}
return this.resultCount = a;
}
/*
*
* 根据ID 查找信息
*/
public Collection getInfoById(int id) {
Collection c = new ArrayList();
for (Iterator it = root.elementIterator(); it.hasNext();) {
Element el = (Element) it.next();
if (el.elementText("id").equals(String.valueOf(id))) {
lm l = new lm();
l.setId(el.elementText("id"));
l.setEmail(el.elementText("email"));
l.setInfo(el.elementText("info"));
l.setName(el.elementText("name"));
l.setTitle(el.elementText("title"));
l.setTel(el.elementText("tel"));
l.setTime(el.elementText("time"));
l.setImg(el.elementText("img"));
for (Iterator i = el.elementIterator("reInfo"); i.hasNext();) {
Element e = (Element) i.next();
l.setPerson(e.elementText("person"));
l.setReplay(e.elementText("replay"));
l.setTotime(e.elementText("totime"));
}
c.add(l);
}
}
return c;
}
/*
* 取得总共的页数
*
*
*/
public int getPageNum() {
if (this.resultCount % this.pageSize == 0)
this.pageNum = this.resultCount / this.pageSize;
else
this.pageNum = this.resultCount / this.pageSize + 1;
return this.pageNum;
}
/*
*
* 取得最大的ID
*
*/
public int getId() {
int aa = this.getResultCount();
int a = 0, result = 0;
;
for (Iterator it = root.elementIterator(); it.hasNext();) {
Element el = (Element) it.next();
a++;
if (a == aa) {
result = Integer.parseInt(el.elementText("id"));
}
}
return result;
}
/*
*
* 取得指定页数的记录并返回 高效分页
*/
public Collection getAll(int CurrPage) {
int b = 0, a = 0;
Collection col = new ArrayList();
for (Iterator it = root.elementIterator(); a < this.pageSize
&& it.hasNext();) {
b++;
Element el = (Element) it.next();
if (b > CurrPage * this.pageSize) {
a++;
lm l = new lm();
l.setId(el.elementText("id"));
l.setEmail(el.elementText("email"));
l.setInfo(el.elementText("info"));
l.setName(el.elementText("name"));
l.setTitle(el.elementText("title"));
l.setTel(el.elementText("tel"));
l.setTime(el.elementText("time"));
l.setImg(el.elementText("img"));
for (Iterator i = el.elementIterator("reInfo"); i.hasNext();) {
Element e = (Element) i.next();
l.setPerson(e.elementText("person"));
l.setReplay(e.elementText("replay"));
l.setTotime(e.elementText("totime"));
}
col.add(l);
}
}
return col;
}
/*
* 在内容中模糊查询
*
*
*/
public Collection search(String str) {
Collection c = new ArrayList();
for (Iterator it = root.elementIterator(); it.hasNext();) {
Element el = (Element) it.next();
if (el.elementText("name").indexOf((str)) != -1) {
lm l = new lm();
l.setId(el.elementText("id"));
l.setEmail(el.elementText("email"));
l.setInfo(el.elementText("info"));
l.setName(el.elementText("name"));
l.setTitle(el.elementText("title"));
l.setTel(el.elementText("tel"));
l.setTime(el.elementText("time"));
l.setImg(el.elementText("img"));
for (Iterator i = el.elementIterator("reInfo"); i.hasNext();) {
Element e = (Element) i.next();
l.setPerson(e.elementText("person"));
l.setReplay(e.elementText("replay"));
l.setTotime(e.elementText("totime"));
}
c.add(l);
}
}
return c;
}
/*
* 根据ID删除元素
*
*/
public void deleById(int id) throws IOException {
for (Iterator it = root.elementIterator(); it.hasNext();) {
Element el = (Element) it.next();
if (el.elementText("id").equals(String.valueOf(id))) {
root.remove(el);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
XMLWriter output = new XMLWriter(new FileWriter(new File(
"c://leavemessage.xml")), format);
output.write(dom);
output.close();
}
}
}
public static void main(String a[]) throws DocumentException {
OpXML o = new OpXML();
/*
* o.getResultCount(); o.getPageNum(); Collection c= o.search("asdf");
* //System.out.print(o.root.nodeCount()); for(Iterator
* it=c.iterator();it.hasNext();) { lm l=(lm)it.next();
* System.out.println(l.getEmail()); System.out.println(l.getTitle()); }
*
* //System.out.print(o.getId());
*/
try {
o.deleById(3);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zgw555555/archive/2007/06/09/1644876.aspx