基于XML語言的來實現(xiàn)購物車的詳細代碼(4)_Xml教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:文檔標準的真實謊言近日,有關(guān)微軟Open XML標準的問題又引發(fā)了某些業(yè)內(nèi)人士的關(guān)注。其一是因為日前ISO(國際標準組織)成員南非和巴西相繼就ISO批準微軟的Open XML成為標準向ISO質(zhì)疑和上訴,理由是日內(nèi)瓦BRM會議并
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的節(jié)點集,如果該節(jié)點集包含的節(jié)點數(shù)為0,則購物車內(nèi)沒有商品,返回true NodeList itemList=myCart.getElementsByTagName(“item”); if(itemList.getLength()==0) return true; else return false; } |
7. 判斷所選商品是否已在購物車內(nèi)
即判斷新傳來商品的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轉(zhuǎn)換成XMLDocument的方法parseString,以及用于輸出時將XSL賦給myCart并返回String型XML字串的 cartTurnToStringWithXSL方法來輔助購物車主要操作的實現(xiàn),這里不再贅述。
分享:XML入門教程:XML DHTML 行為Internet Explorer 5引入了DHTML行為。行為(Behaviors)是一種借助CSS的便利性向HTML元素添加DHTML功能性的方法。 行為 - 什么是行為? IE 5引入了DHTML行為(DHTML behaviors)。行為(Be
相關(guān)Xml教程:
- 相關(guān)鏈接:
- 教程說明:
Xml教程-基于XML語言的來實現(xiàn)購物車的詳細代碼(4)
。