在JSP开发中使用jdom解析临时存放数据的XML文件

在我工作过程中,遇到了对临时存储产品信息的XML文件进行操作的问题.其中就有对XML文件的解析操作,考虑到用DOM或SAX比较麻烦,于是我选择了用jdom进行解析.因为我的XML文件结构比较简单,仅有两层,而且没有复杂的属性,所以没有用到里面太多的方法,只希望能够抛砖引玉,给初学者一点帮助.
下面我就把大概的实现过程说一说.
一.实现解析xml文件的JavaBean(XMLBean):
我把对存放产品信息的xml文档的全部操作都写在了XMLBean()里面,包括添加,修改,删除一条记录,查看相关记录等操作.具体实现的代码如下:
packagexml;
importjava.io.*;
importjava.util.*;
importorg.jdom.*;
importorg.jdom.output.*;
importorg.jdom.input.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
/**
*<p>Title:XMLBean</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2005</p>
*@authorlihs
*@version1.0
**/
/*
**
**通过往XML文件里面添加,删除,修改记录。从而实现对生产部门提交产品信息的处理。
*/
publicclassXMLBean{
privateStringProduceID,ProduceName,ProduceClass,ProduceType,ProduceColor,Baozhiqi,ProduceNum,ProduceDep,ProduceDate;
publicStringgetProduceID(){returnProduceID;}
publicStringgetProduceName(){returnProduceName;}
publicStringgetProduceClass(){returnProduceClass;}
publicStringgetProduceType(){returnProduceType;}
publicStringgetProduceColor(){returnProduceColor;}
publicStringgetBaozhiqi(){returnBaozhiqi;}
publicStringgetProduceNum(){returnProduceNum;}
publicStringgetProduceDep(){returnProduceDep;}
publicStringgetProduceDate(){returnProduceDate;}
publicvoidsetProduceID(Stringproduceid){this.ProduceID=produceid;}
publicvoidsetProduceName(Stringproducename){this.ProduceName=producename;}
publicvoidsetProduceClass(Stringproduceclass){this.ProduceClass=produceclass;}
publicvoidsetProduceType(Stringproducetype){this.ProduceType=producetype;}
publicvoidsetProduceColor(Stringproducecolor){this.ProduceColor=producecolor;}
publicvoidsetBaozhiqi(Stringbaozhiqi){this.Baozhiqi=baozhiqi;}
publicvoidsetProduceNum(Stringproducenum){this.ProduceNum=producenum;}
publicvoidsetProduceDep(Stringproducedep){this.ProduceDep=producedep;}
publicvoidsetProduceDate(Stringproducedate){this.ProduceDate=producedate;}
publicXMLBean(){}
/**
*通过传入路径读取XML文件的内容。
*/
publicVectorLoadXML(Stringpath)throwsException{
VectorxmlVector=null;
FileInputStreamfi=null;
try{
fi=newFileInputStream(path);
xmlVector=newVector();
SAXBuildersb=newSAXBuilder();
Documentdoc=sb.build(fi);
Elementroot=doc.getRootElement();//获取根节点
Listproduces=root.getChildren();//获取根节点下面的所有子元素
Elementproduce=null;
XMLBeanxml=null;
for(inti=0;i<produces.size();i++){
xml=newXMLBean();
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(Exceptione){
System.err.println(e+"error");
}
finally{
try{
fi.close();
}
catch(Exceptione){
e.printStackTrace();
}
}
returnxmlVector;
}
/**
*删除指定的元素信息
*/
publicstaticvoidDelXML(HttpServletRequestrequest)throwsException{
FileInputStreamfi=null;
FileOutputStreamfo=null;
try{
Stringpath=request.getParameter("path");
intxmlid=Integer.parseInt(request.getParameter("id"));
fi=newFileInputStream(path);
SAXBuildersb=newSAXBuilder();
Documentdoc=sb.build(fi);
Elementroot=doc.getRootElement();
Listproduces=root.getChildren();
produces.remove(xmlid);
Stringindent="";
booleannewLines=true;
XMLOutputteroutp=newXMLOutputter(indent,newLines,"GBK");
fo=newFileOutputStream(path);
outp.output(doc,fo);
}
catch(Exceptione){
System.err.println(e+"error");
}
finally{
try{
fi.close();
fo.close();
}
catch(Exceptione){
e.printStackTrace();
}
}
}
/**
*往XML文件中添加一条记录产品信息
**/
publicstaticvoidAddXML(HttpServletRequestrequest)throwsException{
FileInputStreamfi=null;
FileOutputStreamfo=null;
try{
XMLBeanbean=newXMLBean();
Stringpath=request.getParameter("path");
fi=newFileInputStream(path);
SAXBuildersb=newSAXBuilder();
Documentdoc=sb.build(fi);
Elementroot=doc.getRootElement();//
Listproduces=root.getChildren();//
Stringproduceid=bean.toChinese(request.getParameter("ProduceID"));
Stringproducename=bean.toChinese(request.getParameter("ProduceName"));
Stringproduceclass=bean.toChinese(request.getParameter("ProduceClass"));
Stringproducetype=bean.toChinese(request.getParameter("ProduceType"));
Stringproducecolor=bean.toChinese(request.getParameter("ProduceColor"));
Stringbaozhiqi=bean.toChinese(request.getParameter("Baozhiqi"));
Stringproducenum=bean.toChinese(request.getParameter("ProduceNum"));
Stringproducedep=bean.toChinese(request.getParameter("ProduceDep"));
Stringproducedate=bean.toChinese(request.getParameter("ProduceDate"));
Textnewtext;
Elementnewproduce=newElement("Produce");
Elementnewproduceid=newElement("ProduceID");
newproduceid.setText(produceid);
newproduce.addContent(newproduceid);
//
Elementnewproducename=newElement("ProduceName");
newproducename.setText(producename);
newproduce.addContent(newproducename);
//
Elementnewproduceclass=newElement("ProduceClass");
newproduceclass.setText(produceclass);
newproduce.addContent(newproduceclass);
//
Elementnewproducetype=newElement("ProduceType");
newproducetype.setText(producetype);
newproduce.addContent(newproducetype);
//
Elementnewproducecolor=newElement("ProduceColor");
newproducecolor.setText(producecolor);
newproduce.addContent(newproducecolor);
//
Elementnewbaozhiqi=newElement("Baozhiqi");
newbaozhiqi.setText(baozhiqi);
newproduce.addContent(newbaozhiqi);
//
Elementnewproducenum=newElement("ProduceNum");
newproducenum.setText(producenum);
newproduce.addContent(newproducenum);
//
Elementnewproducedep=newElement("ProduceDep");
newproducedep.setText(producedep);
newproduce.addContent(newproducedep);
//
Elementnewproducedate=newElement("ProduceDate");
newproducedate.setText(producedate);
newproduce.addContent(newproducedate);
produces.add(newproduce);//
Stringindent="/n";
booleannewLines=true;
XMLOutputteroutp=newXMLOutputter(indent,newLines,"GBK");
fo=newFileOutputStream(path);
outp.output(doc,fo);
}
catch(Exceptione){
System.err.println(e+"error");
}
finally{
try{
fi.close();
fo.close();
}
catch(Exceptione){
e.printStackTrace();
}
}
}
/**
*更改XML中指定的记录的信息
*/
publicstaticvoidEditXML(HttpServletRequestrequest)throwsException{
FileInputStreamfi=null;
FileOutputStreamfo=null;
try{
XMLBeanbean=newXMLBean();
Stringpath=request.getParameter("path");
intxmlid=Integer.parseInt(request.getParameter("id"));
fi=newFileInputStream(path);
SAXBuildersb=newSAXBuilder();
Documentdoc=sb.build(fi);
Elementroot=doc.getRootElement();//
Listproduces=root.getChildren();//
Elementproduce=(Element)produces.get(xmlid);
Stringproduceid=bean.toChinese(request.getParameter("ProduceID"));
Stringproducename=bean.toChinese(request.getParameter("ProduceName"));
Stringproduceclass=bean.toChinese(request.getParameter("ProduceClass"));
Stringproducetype=bean.toChinese(request.getParameter("ProduceType"));
Stringproducecolor=bean.toChinese(request.getParameter("ProduceColor"));
Stringbaozhiqi=bean.toChinese(request.getParameter("Baozhiqi"));
Stringproducenum=bean.toChinese(request.getParameter("ProduceNum"));
Stringproducedep=bean.toChinese(request.getParameter("ProduceDep"));
Stringproducedate=bean.toChinese(request.getParameter("ProduceDate"));
Textnewtext;
Elementnewproduceid=produce.getChild("ProduceID");
newproduceid.setText(produceid);
//
Elementnewproducename=produce.getChild("ProduceName");
newproducename.setText(producename);
//
Elementnewproduceclass=produce.getChild("ProduceClass");
newproduceclass.setText(produceclass);
//
Elementnewproducetype=produce.getChild("ProduceType");
newproducetype.setText(producetype);
//
Elementnewproducecolor=produce.getChild("ProduceColor");
newproducecolor.setText(producecolor);
//
Elementnewbaozhiqi=produce.getChild("Baozhiqi");
newbaozhiqi.setText(baozhiqi);
//
Elementnewproducenum=produce.getChild("ProduceNum");
newproducenum.setText(producenum);
//
Elementnewproducedep=produce.getChild("ProduceDep");
newproducedep.setText(producedep);
//
Elementnewproducedate=produce.getChild("ProduceDate");
newproducedate.setText(producedate);
//
books.set(xmlid,book);
Stringindent="/n";
booleannewLines=true;
XMLOutputteroutp=newXMLOutputter(indent,newLines,"GBK");
fo=newFileOutputStream(path);
outp.output(doc,fo);
}
catch(Exceptione){
System.err.println(e+"error");
}
finally{
try{
fi.close();
fo.close();
}
catch(Exceptione){
e.printStackTrace();
}
}
}
}
在这些方法中有很多重复的地方,因为是练习没有考虑太多,读者可以有选择的看一下.
二.调用上面写的JavaBean的JSP程序如下:
得到的结果是一个产品列表页面,它包含了XML文件中所有的产品记录,每条记录都有通向该记录详细信息的页面.同时每条记录后面都有查看,编辑,删除的链接,实现的方法都写在了上面的JavaBean里了,在JSP页面里面仅需要传给它相应参数即可.
效果如下:
1.产品列表

2.产品详细信息

<%@pagecontentType="text/html;charset=gb2312"language="java"errorPage=""%>
<%@pagelanguage="java"import="java.util.*,xml.*"%>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<title>产品管理</title>
</head>
<LINKhref="../images/TBspace.css"type=text/cssrel=stylesheet>
<body>
<center><tablewidth="85%"height="96"border="0"align="center">
<tr>
<tdheight="92"><imgsrc="../image/common/produce_head.jpg"width="638"height="90"></td>
</tr>
</table>
<spanclass="style1">录入请求中的产品信息如下</span>
<tableborder="1"cellspacing="0"width="90%"bordercolorlight="#000000"bordercolordark="#FFFFFF"cellpadding="0">
<tr>
<tdwidth="17%"align="center"bgcolor="#D0D0D0">产品编号</td>
<tdwidth="25%"align="center"bgcolor="#D0D0D0">产品名称</td>
<tdwidth="19%"align="center"bgcolor="#D0D0D0">产品类别</td>
<tdwidth="20%"align="center"bgcolor="#D0D0D0">生产部门</td>
<tdalign="center"bgcolor="#D0D0D0">查看</td>
<tdalign="center"bgcolor="#D0D0D0">编辑</td>
<tdalign="center"bgcolor="#D0D0D0">删除</td>
</tr>
<%
Stringpath=application.getRealPath("/")+"produce.xml";
XMLBeanxml=newXMLBean();
Vectorxmlall=xml.LoadXML(path);
for(inti=0;i<xmlall.size();i++){
xml=(XMLBean)xmlall.elementAt(i);
%>
<tr>
<tdwidth="17%"align="center"><%=xml.getProduceID()%></td>
<tdwidth="25%"align="center"><ahref="showproduce.jsp?id=<%=i%>&path=<%=path%>"><%=xml.getProduceName()%></a></td>
<tdwidth="19%"align="center"><%=xml.getProduceClass()%></td>
<tdwidth="20%"align="center"><%=xml.getProduceDep()%></td>
<tdalign="center"><ahref="showproduce.jsp?id=<%=i%>&path=<%=path%>">view</a></td>
<tdalign="center"><ahref="updateproduce.jsp?ProduceID=<%=xml.getProduceID()%>&id=<%=i%>&path=<%=path%>">edit</a></td>
<tdalign="center"><ahref="okdeleteproduce.jsp?id=<%=i%>&path=<%=path%>">delete</a></td>
</tr>
<%}%>
</table>
<inputtype="hidden"name="path"value="<%=path%>">
</center>
</body>
</html>
三.存放产品信息的XML文件produce.xml如下:
<?xmlversion="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>
以上是本人的一点小总结,因为水平有限,不足还请大家谅解,谢谢!

你可能感兴趣的:(jdom)