大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> XML专区 -> 基于XML的购物车的实现

基于XML的购物车的实现

时间: 2021-07-31 作者:daque

购物车是电子商务网站中不行缺乏的构成局部,但暂时大普遍购物车只能动作一个主顾选中商品的展现,存户端没辙将购物车里的实质索取出来满意本人工作处置的须要,而这一点在有些电子商务震动中很有需要。xml的展示使得搜集上传输的数据变得有意旨起来,咱们不妨按照各别的诉求以各别的款式将一个购物车的实质表露出来。

正文将精细领会一个由java实行的鉴于xml的购物车。底下是一个包括了五件商品的购物车的xml内涵构造:它的根元素为cart,total元素表白购物车内的总金额,每个item元素表白一件商品,item里的子元素辨别标领会该商品的简直消息,可按照本质情景增添、窜改或简略。

在这边,须要创造一个表白购物车的类:xmlcart.java,它是一个javabean,以是它包括了一个空的结构因变量。这个类包括了购物车的少许基础功效: 天生一个空的购物车,往购物车里增添商品,简略购物车里的商品,变换购物车内商品的数目以及清空购物车等。它具有一个全部独占变量“private xmldocument mycart”,mycart用来保存购物车里的精细实质,购物车的基础功效即是对它的操纵,它的典型是xmldocument,即一个xml文书档案。如许,对购物车的操纵就变换成对mycart中的子元素的增添、简略,及元素值的计划、窜改等。

1. 清空购物车

清空购物车即天生一个空的购物车。这边空购物车是一个含有根元素cart及其元素total的xml文书档案,total元素是购物车的总金额,它的初始值为0,其xml简直情势如次:

< ?xml version=‘1.0’ encoding=‘gb2312’?>< cart>< total>0< /total>< /cart>将这个xml字符串由parsestring因变量变换成xmldocument惠存mycart。其代码如次:public void emptycart() throws ioexception,saxexception{    string stringcart=“< ?xml version=‘1.0’encoding=‘gb2312’?> ”+       “< cart>< total>0< /total>< /cart>”;      mycart=parsestring(stringcart);    }

2. 增添商品增添商品,行将传入的item元素增添到根元素cart里,个中item里囊括商品精细消息,同声计划total的值。其代码如次:public void additemtocart(string stringitem)throws ioexception,saxexception{//将item由string变换为xmldocumentxmldocument itemadded=parsestring(stringitem);//掏出item节点,并复制它nodelist itemlist=itemadded.getelementsbytagname(“item”);node item=itemlist.item(0);node cloneitem=item.clonenode(true);//即使购物车为空,则结构一个新的购物车if(iscartempty()){     mycart.emptycart();}//即使该商品不在购物车中,则插入该商品,并计划总金额if(!isitemexist(item,mycart)){//取mycart的根元素,并将复制的item节点增添到反面element cartroot=mycart.getdocumentelement();node cartnode=cartroot.appendchild(cloneitem);        computetotal();    //计划总金额        }    }3. 简略商品简略商品,即按照商品代码将该商品的item元素从mycart的根元素cart中简略,并从新计划total的值:public void moveitemfromcart(string id){//掏出以item为单元的节点集cartlist以及根元素cartroot  nodelist cartlist=mycart.getelementsbytagname(“item”);     element cartroot=mycart.getdocumentelement();      //在cartlist中搜索代码为选中id的商品    for(int x=0;x< cartlist.getlength();x++){      node itemnode=cartlist.item(x);      string  idvalue=itemnode.getfirstchild().      getfirstchild().getnodevalue();      //即使找到,则从cartroot中简略该节点,并跳出轮回if(idvalue.equals(id)){      itemnode=cartroot.removechild(itemnode);       break;            }        }        computetotal();    //计划总金额    }4. 变换商品数目按照存户在页面上所填的数目,窜改mycart中quantity,并从新计划total: public void addquantitytocart(string qnty) throws ioexception,saxexception{    //将传过来的包括商品数目的一组xml字符串变换为xml文书档案xmldocument quantitychanged=parsestring(qnty);//掏出包括新数目的quantity节点集和mycart中的quantity节点集nodelist quantitylist=quantitychanged.getelementsbytagname(“quantity”);nodelist cartlist=mycart.getelementsbytagname(“quantity”);//轮回变换商品的数目for(int x=0;x< cartlist.getlength();x++){//将新quantity的值赋给mycart中相映的quantity中去string quantity=quantitylist.item(x).getfirstchild().getnodevalue();cartlist.item(x).getfirstchild().setnodevalue(quantity);}computetotal();    //计划总金额    }5. 计划总金额即计划total的值,个中total=∑(price*quantity): public void computetotal(){    nodelist quantitylist=mycart.getelementsbytagname(“quantity”);    nodelist pricelist=mycart.getelementsbytagname(“price”);    float total=0;    //累加总金额for(int x=0;x< pricelist.getlength();x++){    float quantity=float.parsefloat(quantitylist.item(x)    .getfirstchild().getnodevalue());  float price=float.parsefloat(pricelist.item(x).getfirstchild().getnodevalue());    total=total+quantity*price;    }    //将total附给mycart的totalstring totalstring=string.valueof(total);    mycart.getelementsbytagname(“total”).    item(0).getfirstchild().setnodevalue(totalstring);  }6. 确定购物车能否为空常常在增添新商品时,还须要领会购物车能否为空,即使为空的话,则要天生一个新的购物车。public boolean iscartempty(){//item的节点集,即使该节点集包括的节点数为0,则购物车内没有商品,归来truenodelist itemlist=mycart.getelementsbytagname(“item”);if(itemlist.getlength()==0) return true;else return false;}7. 确定所选商品能否已在购物车内即确定新传来商品的item能否已在mycart中生存,即使生存,归来true。public boolean isitemexist(node item, xmldocument cart){  nodelist itemlist=cart.getelementsbytagname(“item”);      node id=item.getfirstchild();      string idvalue=id.getfirstchild().getnodevalue();      if(itemlist.getlength()!=0){          for(int x=0;x< itemlist.getlength();x++){           node itemtemp = itemlist.item(x);          7node idtemp=itemtemp.getfirstchild();           string idtempvalue=idtemp.getfirstchild().getnodevalue();            if(idvalue.equals(idtempvalue)) return true;            }          return false;        }      return false;    }

除上述本领外,xmlcart还囊括将xml字符串由输时髦的string变换成xmldocument的本领parsestring,以及用来输入时将xsl赋给mycart并归来string型xml字串的 cartturntostringwithxsl本领来扶助购物车重要操纵的实行,这边不复赘述。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.