|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
到时我们不用学struts,不用学spring,不用学Hibernate,只要能把jsf学会了,完全可以替代所有的框架,包括AJAX,都知道AJAX并不是新技术,虽说我没深入学习jsf但我认为jsf应该已经能通过其它技术替代AJAX,实现无缝刷新。dom|js|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="
";
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="
";
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页面内里仅必要传给它响应参数便可.
效果以下:
<%@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="http://www.webjx.com/htmldata/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>
以上是自己的一点小总结,由于程度无限,不敷还请人人体谅,感谢!
令人可喜的是java现在已经开源了,所以我想我上述的想法也许有一天会实现,因为java一直都是不断创新的语言,每次创新都会给我们惊喜,这也是我喜欢java的一个原因。 |
|