国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JSP > 正文

在JSP開(kāi)發(fā)中使用jdom解析臨時(shí)存放數(shù)據(jù)的XML文件

2024-09-05 00:20:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

    在我工作過(guò)程中,遇到了對(duì)臨時(shí)存儲(chǔ)產(chǎn)品信息的xml文件進(jìn)行操作的問(wèn)題.其中就有對(duì)xml文件的解析操作,考慮到用dom或sax比較麻煩,于是我選擇了用jdom進(jìn)行解析.因?yàn)槲业膞ml文件結(jié)構(gòu)比較簡(jiǎn)單,僅有兩層,而且沒(méi)有復(fù)雜的屬性,所以沒(méi)有用到里面太多的方法,只希望能夠拋磚引玉,給初學(xué)者一點(diǎn)幫助.

    下面我就把大概的實(shí)現(xiàn)過(guò)程說(shuō)一說(shuō).

    一.實(shí)現(xiàn)解析xml文件的javabean(xmlbean):

    我把對(duì)存放產(chǎn)品信息的xml文檔的全部操作都寫(xiě)在了xmlbean()里面,包括添加,修改,刪除一條記錄,查看相關(guān)記錄等操作.具體實(shí)現(xiàn)的代碼如下:

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
**/
/*
**
**通過(guò)往xml文件里面添加,刪除,修改記錄。從而實(shí)現(xiàn)對(duì)生產(chǎn)部門(mén)提交產(chǎn)品信息的處理。
*/
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(){}
/**
* 通過(guò)傳入路徑讀取xml文件的內(nèi)容。
*/
    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(); //獲取根節(jié)點(diǎn)
            list produces = root.getchildren(); //獲取根節(jié)點(diǎn)下面的所有子元素
            element produce =null;
            xmlbean xml =null;
            for(int i=0;i<produces.size();i++){
                xml = new xmlbean();
                produce = (element)produces.get(i ); //取得指定的孩子節(jié)點(diǎn)信息
                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文件中添加一條記錄產(chǎn)品信息
**/
    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();
                }
        }
    }
}

  在這些方法中有很多重復(fù)的地方,因?yàn)槭蔷毩?xí)沒(méi)有考慮太多,讀者可以有選擇的看一下.

    二.調(diào)用上面寫(xiě)的javabean的jsp程序如下:

    得到的結(jié)果是一個(gè)產(chǎn)品列表頁(yè)面,它包含了xml文件中所有的產(chǎn)品記錄,每條記錄都有通向該記錄詳細(xì)信息的頁(yè)面.同時(shí)每條記錄后面都有查看,編輯,刪除的鏈接,實(shí)現(xiàn)的方法都寫(xiě)在了上面的javabean里了,在jsp頁(yè)面里面僅需要傳給它相應(yīng)參數(shù)即可.

    效果如下:

<%@ 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>產(chǎn)品管理</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://m.survivalescaperooms.com/htmldata/upload/200902/common/produce_head.jpg" width="638" height="90"></td>
  </tr>
</table>
  <span class="style1">錄入請(qǐng)求中的產(chǎn)品信息如下</span>
<table border="1" cellspacing="0" width="90%" bordercolorlight="#000000" bordercolordark="#ffffff" cellpadding="0">
    <tr>
        <td width="17%"  align="center" bgcolor="#d0d0d0" >產(chǎn)品編號(hào)</td>
        <td width="25%"  align="center" bgcolor="#d0d0d0" >產(chǎn)品名稱(chēng)</td>
        <td width="19%"  align="center"  bgcolor="#d0d0d0">產(chǎn)品類(lèi)別</td>
        <td width="20%"  align="center" bgcolor="#d0d0d0">生產(chǎn)部門(mén)</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>

    三.存放產(chǎn)品信息的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>第二事業(yè)部</producedep>
  <producedate>2005-05-26</producedate>
 </produce>
 <produce>....</produce>
</produces>

  以上是本人的一點(diǎn)小總結(jié),因?yàn)樗接邢?不足還請(qǐng)大家諒解,謝謝!



發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新巴尔虎右旗| 沽源县| 江门市| 库尔勒市| 平湖市| 淅川县| 承德市| 常宁市| 洛南县| 富阳市| 盐边县| 富锦市| 肥城市| 弥渡县| 礼泉县| 侯马市| 尚志市| 丰镇市| 积石山| 澄城县| 衢州市| 乌恰县| 盐边县| 卢龙县| 永顺县| 固安县| 广丰县| 贡嘎县| 杭锦后旗| 马关县| 宜良县| 赤城县| 环江| 兴化市| 蒲江县| 临颍县| 周至县| 上饶县| 泸定县| 永和县| 武陟县|