Using Jdom in JSP development to parse an XML file that temporarily holds data

Source: Internet
Author: User
Tags add gettext integer return string version
dom|js|xml| Data

In the course of my work, the problem of operating an XML file that temporarily stores product information is encountered. There are parsing operations on XML files, given the difficulty of using DOM or sax, so I chose to parse with Jdom. Because my XML file structure is simpler, with only two layers, And there is no complex attribute, so there is no use of the inside too many methods, just hope to be able to give beginners a little help.

Let me say a few words about the realization process.

I. Implementing JavaBean (Xmlbean) for parsing XML files:

I write all of the XML documents that hold the product information in Xmlbean (), including adding, modifying, deleting a record, viewing related records, and so on. The following code is implemented:

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


**/


/*


**


* * Add, delete, and modify records through the XML file. In order to achieve the production department to submit product information processing.


*/


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 () {}


/**


* Reads the contents of an XML file through an incoming path.


*/


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 (); Get root node


List produces = Root.getchildren (); Gets all the child elements under the root node


Element produce =null;


xmlbean XML =null;


for (int i=0;i<produces.size (); i++) {


XML = new Xmlbean ();


produce = (Element) produces.get (i); Gets the specified child node information


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;


    }


/**


* Deletes the specified element information


*/


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 ();


                }


        }


    }


/**


* Add a record to the XML file product information


**/


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 ();


                }


            }


    }


/**


* Change information for the records specified in the 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 ();


                }


        }


    }


}

There is a lot of repetition in these methods, because it is not too much to take into account, the reader can have a choice to look at.

two. The JSP program that invokes the JavaBean written above is as follows:

The result is a product List page, it contains all the product records in the XML file, and each record has a page that leads to the details of the record. At the same time, each record is followed by a view, edit, delete link, the implementation of the method are written in the top of the JavaBean, In the JSP page only need to pass to its corresponding parameters.

The effect is as follows:

<%@ 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> Product Management </title>


</head>


<link href= ". /images/tbspace.css "Type=text/css rel=stylesheet>


<body>


<center><table width= "85%" height= "border=" "0" align= "center" >


<tr>


<td height= "><img src=" http://www.webjx.com/htmldata/image/common/produce_head.jpg "width=" "638" height= "
" ></td>

&lt;/tr&gt;


&lt;/table&gt;


&lt;span class= "style1" &gt; Input request product information as follows &lt;/span&gt;


&lt;table border= "1" cellspacing= "0" width= "90%" bordercolorlight= "#000000" bordercolordark= "#FFFFFF" cellpadding= "0" &gt;


&lt;tr&gt;


&lt;td width= "17%" align= "center" bgcolor= "#D0D0D0" &gt; Product number &lt;/td&gt;


&LT;TD width= "25%" align= "center" bgcolor= "#D0D0D0" &gt; Product name &lt;/td&gt;


&lt;td width= "19%" align= "center" bgcolor= "#D0D0D0" &gt; Product category &lt;/td&gt;


&lt;td width= "20%" align= "center" bgcolor= "#D0D0D0" &gt; Production Department &lt;/td&gt;


&lt;td align= "center" bgcolor= "#D0D0D0" &gt; View &lt;/td&gt;


&lt;td align= "center" bgcolor= "#D0D0D0" &gt; Edit &lt;/td&gt;


&lt;td align= "center" bgcolor= "#D0D0D0" &gt; Delete &lt;/td&gt;


&lt;/tr&gt;


&lt;%


String Path =application.getrealpath ("/") + "Produce.xml";


Xmlbean xml=new Xmlbean ();


Vector Xmlall=xml. Loadxml (path);


for (int i=0;i&lt;xmlall.size (); i++) {


xml= (Xmlbean) xmlall.elementat (i);


%&gt;


&lt;tr&gt;


&lt;td width= "17%" align= "center" &gt;&lt;%=xml.getproduceid ()%&gt;&lt;/td&gt;


&lt;td width= "25%" align= "center" &gt;&lt;a href= "showproduce.jsp?id=&lt;%=i%&gt;&amp;path=&lt;%=path%&gt;" &gt;&lt;%=xml.getproducename ()%&gt;&lt;/a&gt;&lt;/td&gt;


&lt;td width= "19%" align= "center" &gt;&lt;%=xml.getproduceclass ()%&gt;&lt;/td&gt;


&lt;td width= "20%" align= "center" &GT;&LT;%=XML.GETPRODUCEDEP ()%&gt;&lt;/td&gt;


&lt;td align= "center" &gt;&lt;a href= "showproduce.jsp?id=&lt;%=i%&gt;&amp;path=&lt;%=path%&gt;" &gt;view&lt;/a &gt;&lt;/td&gt;


&lt;td align= "center" &gt;&lt;a href= "updateproduce.jsp"? Produceid=&lt;%=xml.getproduceid ()%&gt;&amp;id=&lt;%=i%&gt;&amp;path=&lt;%=path%&gt; "&gt;edit&lt;/a&gt;&lt;/td &gt;


&lt;td align= "center" &gt;&lt;a href= "okdeleteproduce.jsp?id=&lt;%=i%&gt;&amp;path=&lt;%=path%&gt;" &gt;delete &lt;/a&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;%}%&gt;


&lt;/table&gt;


&lt;input type= "hidden" name= "path" value= "&lt;%=path%&gt;" &gt;


&lt;/center&gt;


&lt;/body&gt;


&lt;/html&gt;

three. The XML file containing the product information is produce.xml as follows:

<?xml version= "1.0" encoding= "GBK"?>
<Produces>
<Produce>
<ProduceID>PW0005</ProduceID>
<PRODUCENAME>CD Green Poison Lady 50ml</producename>
<ProduceClass> Women </ProduceClass>
<ProduceType>50ml</ProduceType>
<ProduceColor> Pink </ProduceColor>
<Baozhiqi>5</Baozhiqi>
<ProduceNum>480</ProduceNum>
<ProduceDep> Second Division </ProduceDep>
<ProduceDate>2005-05-26</ProduceDate>
</Produce>
<Produce>....</Produce>
</Produces>

Above is my little summary, because the level is limited, insufficient also please everybody understanding, thank you!







Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.