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>
</tr>
</table>
<span class= "style1" > Input request product information 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 number </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>
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!