免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
在JSP開發(fā)中使用jdom解析臨時(shí)存放數(shù)據(jù)的XML文件
    在我工作過程中,遇到了對(duì)臨時(shí)存儲(chǔ)產(chǎn)品信息的XML文件進(jìn)行操作的問題.其中就有對(duì)XML文件的解析操作,考慮到用DOM或SAX比較麻煩,于是我選擇了用jdom進(jìn)行解析.因?yàn)槲业腦ML文件結(jié)構(gòu)比較簡(jiǎn)單,僅有兩層,而且沒有復(fù)雜的屬性,所以沒有用到里面太多的方法,只希望能夠拋磚引玉,給初學(xué)者一點(diǎn)幫助.
    下面我就把大概的實(shí)現(xiàn)過程說一說.
    一.實(shí)現(xiàn)解析xml文件的JavaBean(XMLBean):
    我把對(duì)存放產(chǎn)品信息的xml文檔的全部操作都寫在了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
**/
/*
**
**通過往XML文件里面添加,刪除,修改記錄。從而實(shí)現(xiàn)對(duì)生產(chǎ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(){}
/**
* 通過傳入路徑讀取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í)沒有考慮太多,讀者可以有選擇的看一下.
    二.調(diào)用上面寫的JavaBean的JSP程序如下:
    得到的結(jié)果是一個(gè)產(chǎn)品列表頁面,它包含了XML文件中所有的產(chǎn)品記錄,每條記錄都有通向該記錄詳細(xì)信息的頁面.同時(shí)每條記錄后面都有查看,編輯,刪除的鏈接,實(shí)現(xiàn)的方法都寫在了上面的JavaBean里了,在JSP頁面里面僅需要傳給它相應(yīng)參數(shù)即可.
    效果如下:
1.產(chǎn)品列表

2.產(chǎn)品詳細(xì)信息

<%@ 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="../image/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)品名稱</td>
        <td width="19%"  align="center"  bgcolor="#D0D0D0">產(chǎn)品類別</td>
        <td width="20%"  align="center" bgcolor="#D0D0D0">生產(chǎ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)大家諒解,謝謝!


本文引用通告地址: http://blog.csdn.net/chensheng913/services/trackbacks/407148.aspx
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
用JDOM操作XML文件
將數(shù)據(jù)庫表里的數(shù)據(jù)直接轉(zhuǎn)為XML文件
Java操作XML文件 dom4j 篇
使用反射簡(jiǎn)化繁瑣的對(duì)對(duì)象成員設(shè)值取值的過程
如何在java中使用jdom生成和解析xml文件
Dom4J解析xml文檔
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服