当前位置: 移动技术网 > IT编程>网页制作>XML > 基于XML的购物车的实现

基于XML的购物车的实现

2017年12月08日  | 移动技术网IT编程  | 我要评论

eurofx,直销模式,逃脱本色1-13

购物车是电子商务网站中不可缺少的组成部分,但目前大多数购物车只能作为一个顾客选中商品的展示,客户端无法将购物车里的内容提取出来满足自己事务处理的需要,而这一点在有些电子商务活动中很有必要。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转换为xmldocument
xmldocument 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的total
string totalstring=string.valueof(total);
    mycart.getelementsbytagname(“total”).
    item(0).getfirstchild().setnodevalue(totalstring);
  }
6. 判断购物车是否为空
通常在添加新商品时,还需要知道购物车是否为空,
如果为空的话,则要生成一个新的购物车。
public boolean iscartempty(){
//item的节点集,如果该节点集包含的节点数为0,则购物车内没有商品,返回true
nodelist 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方法来辅助购物车主要操作的实现,这里不再赘述。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网