在工作過程中,遇到了對臨時儲存產品資訊的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頁面裡面僅需要傳給它相應參數即可.
效果如下:
1.產品列表
2.產品詳細資料
<%@ 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>
以上是本人的一點小總結,因為水平有限,不足還請大家諒解,謝謝!