sp; 在我工作過程中,遇到了對臨時存儲產品信息的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());nbsp; 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"));
sp; 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"));
面.同時每條記錄後面都有查看,編輯,刪除的鏈接,實現的方法都寫在了上面的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="../image/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>
以上是本人的一點小總結,因為水平有限,不足還請大家諒解,謝謝