使用JSP + JAVABEAN + XML 开发的一个例子__教程 |
|
日期:2007-5-20 1:24:45 人气:185 [大 中 小] |
|
|
|
restaurants =doc.getElementsByTagName("restaurant");
//根据外部传入的参数来决定对 restaurant.xml 文件的操作 if (strAct!=null){ if(strAct.equals("addnewDo")){
String strName; String strPhone; String strAddress; Text textseg;
strName=(String)request.getParameter("name").trim(); strPhone=(String)request.getParameter("phone").trim(); strAddress=(String)request.getParameter("address").trim();
//数据校验 if(strName==null){ out.print(showDialog("餐馆名称不能为空!")); return; } if(strPhone==null){ out.print(showDialog("餐馆电话不能为空!")); return; } /*if(strAddress==null){ out.print(showDialog("餐馆地址不能为空!")); return; }*/
//校验数据的唯一性 for(int i=0;i<restaurants.getLength();i++){ Element restaurant=(Element) restaurants.item(i); if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strName)){ out.print(showDialog("餐馆名称重复!")); return; }else{ if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strPhone)){ out.print(showDialog("餐馆电话重复!")); return; } }
}
//得到已有的记录数,给新增的餐馆记录设定唯一的递增的id 属性 int intNum=0; Element restNum=(Element)doc.getElementsByTagName("restaurants").item(0); intNum=Integer.parseInt(restNum.getAttributeNode("num").getNodeValue());
intNum+=1;
//为restaurants的属性num 的数值加1 restNum.getAttributeNode("num").setNodeValue(String.valueOf(intNum));
//新增节点 Element newRestaurant=doc.createElement("restaurant");
Attr newArrId=doc.createAttribute("id"); //Attribute newArrId = new Attribute("id",String.valueOf(intNum)); textseg=doc.createTextNode(String.valueOf(intNum)); newArrId.setValue(String.valueOf(intNum)); newRestaurant.setAttributeNode(newArrId);
Element newName=doc.createElement("name"); textseg=doc.createTextNode(strName); newName.appendChild(textseg); newRestaurant.appendChild(newName);
Element newPhone=doc.createElement("phone"); textseg=doc.createTextNode(strPhone); newPhone.appendChild(textseg); newRestaurant.appendChild(newPhone);
Element newAddress=doc.createElement("address"); textseg=doc.createTextNode(strAddress); newAddress.appendChild(textseg); newRestaurant.appendChild(newAddress);
doc.getDocumentElement().appendChild(newRestaurant);
//调用bean 写入相应的xml文件 writeXmlBean.writeXml(doc,"webapps/canyin/data/restaurants.xml");
response.sendRedirect(request.getRequestURI()); return; } if(strAct.equals("modiDo")){ String strName; String strPhone; String strAddress; Text textseg; int modiId; //记录要修改的记录是item(i)的哪一项 int intI=0;
strName=(String)request.getParameter("name").trim(); strPhone=(String)request.getParameter("phone").trim(); strAddress=(String)request.getParameter("address").trim(); modiId=Integer.parseInt(request.getParameter("recordId").trim()); |
|
出处:本站原创 作者:佚名 |
|
|