在JSP开发中使用jdom解析临时存放数据的XML文件 (转)

在工作过程中,遇到了对临时存储产品信息的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>
以上是本人的一点小总结,因为水平有限,不足还请大家谅解,谢谢!

你可能感兴趣的:(在JSP开发中使用jdom解析临时存放数据的XML文件 (转))