During the work process, the XML file for temporary storage product information was operated. there is an XML file parsing operation. Considering Dom or Sax is troublesome, I chose JDOM for parsing. because my XML file structure is relatively simple, there are only two layers, and there are no complex attributes, so there are not too many methods in it. I just want to give some help to beginners.
Next I will talk about the general implementation process.
1. Implement the parsing of the XML file's JavaBean (xmlbean ):
I wrote all the operations on the XML document that stores product information in xmlbean (), including adding, modifying, deleting a record, and viewing related records. the specific implementation code is as follows:
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 to the XML file. This allows the production department to submit product information.
*/
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 (){}
/**
* Read the content of the XML file through the input 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 the root node
List produces = root. getchildren (); // obtain all 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); // get 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;
}
/**
* Delete 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 product information to the XML file
**/
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 ();
}
}
}
/**
* Modify the information of a specified record in 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 are many repetitions in these methods, because the exercises are not considered too much, readers can have a choice.
2. the JSP program that calls the an written above is as follows:
The result is a product list page that contains all the product records in the XML file. Each record has a page for detailed information about the record. at the same time, each record is followed by a view, edit, or delete link. The implementation method is written in the JavaBean above. On the JSP page, you only need to pass the corresponding parameter to it.
The effect is as follows:
1. product list
2. product details
<% @ 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 = "96" border = "0" align = "center">
<Tr>
<TD Height = "92"> </TD>
</Tr>
</Table>
<SPAN class = "style1"> the product information in the input request is as follows </span>
<Table border = "1" cellspacing = "0" width = "90%" bordercolorlight = "#000000" bordercolordark = "# ffffff" cellpadding = "0">
<Tr>
<TD width = "17%" align = "center" bgcolor = "# d0d0d0"> product no. </TD>
<TD width = "25%" align = "center" bgcolor = "# d0d0d0"> product name </TD>
<TD width = "19%" align = "center" bgcolor = "# d0d0d0"> product category </TD>
<TD width = "20%" align = "center" bgcolor = "# d0d0d0"> Production Department </TD>
<TD align = "center" bgcolor = "# d0d0d0"> View </TD>
<TD align = "center" bgcolor = "# d0d0d0"> edit </TD>
<TD align = "center" bgcolor = "# d0d0d0"> Delete </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>
3. the XML file produce. XML for storing product information is as follows:
<? XML version = "1.0" encoding = "GBK"?>
<Produces>
<Produce>
<Produceid> pw0005 </produceid>
<Producename> MS. CD green virus 50 ml </producename>
<Produceclass> Women's </produceclass>
<Producetype> 50 ml </producetype>
<Producecolor> Pink </producecolor>
<Baozhiqi> 5 </baozhiqi>
<Producenum> 480 </producenum>
<Producedep> Business Unit 2 </producedep>
<Producedate> 2005-05-26 </producedate>
</Produce>
<Produce>... </produce>
</Produces>
The above is a small Summary of myself. Please forgive me for my limited level and shortcomings. Thank you!