在我工作过程中,遇到了对临时存储产品信息的xml文件进行操作的问题.其中就有对xml文件的解析操作,考虑到用dom或sax比较麻烦,于是我选择了用jdom进行解析.因为我的xml文件结构比较简单,仅有两层,而且没有复杂的属性,所以没有用到里面太多的方法,只希望能够抛砖引玉,给初学者一点帮助.
下面我就把大概的实现过程说一说.
一.实现解析xml文件的javabean(xmlbean):
我把对存放产品信息的xml文档的全部操作都写在了xmlbean()里面,包括添加,修改,删除一条记录,查看相关记录等操作.具体实现的代码如下:
package xml;
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* <p>title:xmlbean</p>
* <p>description: </p>
* <p>copyright: copyright (c) 2005</p>
* @author lihs
* @version 1.0
**/
/*
**
**通过往xml文件里面添加,删除,修改记录。从而实现对生产部门提交产品信息的处理。
*/
public class xmlbean{
private string produceid,producename,produceclass,producetype,producecolor,baozhiqi,producenum,producedep,producedate;
public string getproduceid() { return produceid;}
public string getproducename() { return producename;}
public string getproduceclass() { return produceclass;}
public string getproducetype() { return producetype;}
public string getproducecolor() { return producecolor;}
public string getbaozhiqi() { return baozhiqi;}
public string getproducenum() { return producenum;}
public string getproducedep() { return producedep;}
public string getproducedate() { return producedate;}
public void setproduceid(string produceid) { this.produceid =produceid ; }
public void setproducename(string producename) { this.producename =producename; }
public void setproduceclass(string produceclass) { this.produceclass =produceclass ; }
public void setproducetype(string producetype) { this.producetype =producetype ; }
public void setproducecolor(string producecolor) { this.producecolor =producecolor ; }
public void setbaozhiqi(string baozhiqi) { this.baozhiqi =baozhiqi ; }
public void setproducenum(string producenum) { this.producenum =producenum ; }
public void setproducedep(string producedep) { this.producedep =producedep ; }
public void setproducedate(string producedate) { this.producedate =producedate ; }
public xmlbean(){}
/**
* 通过传入路径读取xml文件的内容。
*/
public vector loadxml(string path)throws exception{
vector xmlvector = null;
fileinputstream fi = null;
try{
fi = new fileinputstream(path);
xmlvector = new vector();
saxbuilder sb = new saxbuilder();
document doc = sb.build(fi);
element root = doc.getrootelement(); //获取根节点
list produces = root.getchildren(); //获取根节点下面的所有子元素
element produce =null;
xmlbean xml =null;
for(int i=0;i<produces.size();i++){
xml = new xmlbean();
produce = (element)produces.get(i ); //取得指定的孩子节点信息
xml.setproduceid(produce.getchild("produceid").gettext());
xml.setproducename(produce.getchild("producename").gettext());
xml.setproduceclass(produce.getchild("produceclass").gettext());
xml.setproducetype(produce.getchild("producetype").gettext());
xml.setproducecolor(produce.getchild("producecolor").gettext());
xml.setbaozhiqi(produce.getchild("baozhiqi").gettext());
xml.setproducenum(produce.getchild("producenum").gettext());
xml.setproducedep(produce.getchild("producedep").gettext());
xml.setproducedate(produce.getchild("producedate").gettext());
xmlvector.add(xml);
}
}
catch(exception e){
system.err.println(e+"error");
}
finally{
try{
fi.close();
}
catch(exception e){
e.printstacktrace();
}
}
return xmlvector;
}
/**
* 删除指定的元素信息
*/
public static void delxml(httpservletrequest request)throws exception{
fileinputstream fi = null;
fileoutputstream fo = null;
try{
string path=request.getparameter("path");
int xmlid=integer.parseint(request.getparameter("id"));
fi = new fileinputstream(path);
saxbuilder sb = new saxbuilder();
document doc = sb.build(fi);
element root = doc.getrootelement();
list produces = root.getchildren();
produces.remove(xmlid);
string indent = "";
boolean newlines = true;
xmloutputter outp = new xmloutputter(indent,newlines,"gbk");
fo=new fileoutputstream(path);
outp.output(doc,fo);
}
catch(exception e){
system.err.println(e+"error");
}
finally{
try{
fi.close();
fo.close();
}
catch(exception e){
e.printstacktrace();
}
}
}
/**
* 往xml文件中添加一条记录产品信息
**/
public static void addxml(httpservletrequest request)throws exception{
fileinputstream fi = null;
fileoutputstream fo = null;
try{
xmlbean bean=new xmlbean();
string path=request.getparameter("path");
fi = new fileinputstream(path);
saxbuilder sb = new saxbuilder();
document doc = sb.build(fi);
element root = doc.getrootelement(); //
list produces = root.getchildren(); //
string produceid=bean.tochinese(request.getparameter("produceid"));
string producename=bean.tochinese(request.getparameter("producename"));
string produceclass=bean.tochinese(request.getparameter("produceclass"));
string producetype=bean.tochinese(request.getparameter("producetype"));
string producecolor=bean.tochinese(request.getparameter("producecolor"));
string baozhiqi=bean.tochinese(request.getparameter("baozhiqi"));
string producenum=bean.tochinese(request.getparameter("producenum"));
string producedep=bean.tochinese(request.getparameter("producedep"));
string producedate=bean.tochinese(request.getparameter("producedate"));
text newtext;
element newproduce= new element("produce");
element newproduceid= new element("produceid");
newproduceid.settext(produceid);
newproduce.addcontent(newproduceid);
//
element newproducename= new element("producename");
newproducename.settext(producename);
newproduce.addcontent(newproducename);
//
element newproduceclass= new element("produceclass");
newproduceclass.settext(produceclass);
newproduce.addcontent(newproduceclass);
//
element newproducetype= new element("producetype");
newproducetype.settext(producetype);
newproduce.addcontent(newproducetype);
//
element newproducecolor= new element("producecolor");
newproducecolor.settext(producecolor);
newproduce.addcontent(newproducecolor);
//
element newbaozhiqi= new element("baozhiqi");
newbaozhiqi.settext(baozhiqi);
newproduce.addcontent(newbaozhiqi);
//
element newproducenum= new element("producenum");
newproducenum.settext(producenum);
newproduce.addcontent(newproducenum);
//
element newproducedep= new element("producedep");
newproducedep.settext(producedep);
newproduce.addcontent(newproducedep);
//
element newproducedate= new element("producedate");
newproducedate.settext(producedate);
newproduce.addcontent(newproducedate);
produces.add(newproduce);//
string indent = "/n";
boolean newlines = true;
xmloutputter outp = new xmloutputter(indent,newlines,"gbk");
fo=new fileoutputstream(path);
outp.output(doc,fo);
}
catch(exception e){
system.err.println(e+"error");
}
finally{
try{
fi.close();
fo.close();
}
catch(exception e){
e.printstacktrace();
}
}
}
/**
* 更改xml中指定的记录的信息
*/
public static void editxml(httpservletrequest request)throws exception{
fileinputstream fi = null;
fileoutputstream fo = null;
try{
xmlbean bean=new xmlbean();
string path=request.getparameter("path");
int xmlid=integer.parseint(request.getparameter("id"));
fi = new fileinputstream(path);
saxbuilder sb = new saxbuilder();
document doc = sb.build(fi);
element root = doc.getrootelement(); //
list produces = root.getchildren(); //
element produce=(element)produces.get(xmlid);
string produceid=bean.tochinese(request.getparameter("produceid"));
string producename=bean.tochinese(request.getparameter("producename"));
string produceclass=bean.tochinese(request.getparameter("produceclass"));
string producetype=bean.tochinese(request.getparameter("producetype"));
string producecolor=bean.tochinese(request.getparameter("producecolor"));
string baozhiqi=bean.tochinese(request.getparameter("baozhiqi"));
string producenum=bean.tochinese(request.getparameter("producenum"));
string producedep=bean.tochinese(request.getparameter("producedep"));
string producedate=bean.tochinese(request.getparameter("producedate"));
text newtext;
element newproduceid= produce.getchild("produceid");
newproduceid.settext(produceid);
//
element newproducename=produce.getchild("producename");
newproducename.settext(producename);
//
element newproduceclass=produce.getchild("produceclass");
newproduceclass.settext(produceclass);
//
element newproducetype=produce.getchild("producetype");
newproducetype.settext(producetype);
//
element newproducecolor=produce.getchild("producecolor");
newproducecolor.settext(producecolor);
//
element newbaozhiqi= produce.getchild("baozhiqi");
newbaozhiqi.settext(baozhiqi);
//
element newproducenum=produce.getchild("producenum");
newproducenum.settext(producenum);
//
element newproducedep=produce.getchild("producedep");
newproducedep.settext(producedep);
//
element newproducedate=produce.getchild("producedate");
newproducedate.settext(producedate);
//
books.set(xmlid,book);
string indent = "/n";
boolean newlines = true;
xmloutputter outp = new xmloutputter(indent,newlines,"gbk");
fo=new fileoutputstream(path);
outp.output(doc,fo);
}
catch(exception e){
system.err.println(e+"error");
}
finally{
try{
fi.close();
fo.close();
}
catch(exception e){
e.printstacktrace();
}
}
}
}
在这些方法中有很多重复的地方,因为是练习没有考虑太多,读者可以有选择的看一下.
二.调用上面写的javabean的jsp程序如下:
得到的结果是一个产品列表页面,它包含了xml文件中所有的产品记录,每条记录都有通向该记录详细信息的页面.同时每条记录后面都有查看,编辑,删除的链接,实现的方法都写在了上面的javabean里了,在jsp页面里面仅需要传给它相应参数即可.
效果如下:
<%@ page contenttype="text/html; charset=gb2312" language="java" errorpage="" %>
<%@ page language="java" import="java.util.*,xml.*"%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>产品管理</title>
</head>
<link href="../images/tbspace.css" type=text/css rel=stylesheet>
<body>
<center><table width="85%" height="96" border="0" align="center">
<tr>
<td height="92"><img src="http://www.VeVb.com/htmldata/upload/200902/common/produce_head.jpg" width="638" height="90"></td>
</tr>
</table>
<span class="style1">录入请求中的产品信息如下</span>
<table border="1" cellspacing="0" width="90%" bordercolorlight="#000000" bordercolordark="#ffffff" cellpadding="0">
<tr>
<td width="17%" align="center" bgcolor="#d0d0d0" >产品编号</td>
<td width="25%" align="center" bgcolor="#d0d0d0" >产品名称</td>
<td width="19%" align="center" bgcolor="#d0d0d0">产品类别</td>
<td width="20%" align="center" bgcolor="#d0d0d0">生产部门</td>
<td align="center" bgcolor="#d0d0d0" >查看</td>
<td align="center" bgcolor="#d0d0d0">编辑</td>
<td align="center" bgcolor="#d0d0d0">删除</td>
</tr>
<%
string path =application.getrealpath("/")+"produce.xml";
xmlbean xml=new xmlbean();
vector xmlall=xml.loadxml(path);
for(int i=0;i<xmlall.size();i++){
xml=(xmlbean)xmlall.elementat(i );
%>
<tr>
<td width="17%" align="center" ><%=xml.getproduceid()%></td>
<td width="25%" align="center" ><a href="showproduce.jsp?id=<%=i%>&path=<%=path%>"><%=xml.getproducename()%></a></td>
<td width="19%" align="center" ><%=xml.getproduceclass()%></td>
<td width="20%" align="center" ><%=xml.getproducedep()%></td>
<td align="center" ><a href="showproduce.jsp?id=<%=i%>&path=<%=path%>">view</a></td>
<td align="center" ><a href="updateproduce.jsp?produceid=<%=xml.getproduceid()%>&id=<%=i%>&path=<%=path%>">edit</a></td>
<td align="center" ><a href="okdeleteproduce.jsp?id=<%=i%>&path=<%=path%>">delete</a></td>
</tr>
<%}%>
</table>
<input type="hidden" name="path" value="<%=path%>">
</center>
</body>
</html>
三.存放产品信息的xml文件produce.xml如下:
<?xml version="1.0" encoding="gbk"?>
<produces>
<produce>
<produceid>pw0005</produceid>
<producename>cd绿毒女士 50ml</producename>
<produceclass>女式</produceclass>
<producetype>50ml</producetype>
<producecolor>粉红</producecolor>
<baozhiqi>5</baozhiqi>
<producenum>480</producenum>
<producedep>第二事业部</producedep>
<producedate>2005-05-26</producedate>
</produce>
<produce>....</produce>
</produces>
以上是本人的一点小总结,因为水平有限,不足还请大家谅解,谢谢!
新闻热点
疑难解答