Shopping carts are an indispensable part of the e-commerce website, but most shopping carts can only be displayed as a customer to select the product, the client cannot extract the contents of the shopping cart to meet the needs of their own transaction, and this It is necessary in some e-commerce activities. The emergence of XML makes data transmitted on the network make it meaningful, and we can display the contents of a shopping cart according to different requirements. This article will analyze an XML-based shopping cart implemented by Java. Below is an intrinsic structure of a shopping cart containing five items: its root element is Cart, TOTAL element indicates the total amount of the shopping cart, each Item element represents a commodity, the child elements in the item indicate respectively. The specific information of this item can be added, modified or deleted depending on the actual situation. Here, you need to create a class indicating a shopping cart: Xmlcart.java, which is a JavaBean, so it contains an empty constructor. This class contains some basic functions of the shopping cart: generate an empty shopping cart, add goods to the shopping cart, delete the goods in the shopping cart, change the number of goods in the shopping cart and empty shopping carts. It has a global private variable "private xmldocument mycart", MyCart is used to store details in the shopping cart, the basic function of the shopping cart is to operate on it, its type is XMLDocument, an XML document. In this way, the operation of the shopping cart is converted into the addition, deletion, and elements of the child elements in mycart, modification, and the like. 1. Empty shopping carts and empty shopping carts to generate an empty shopping cart. This empty shopping cart is an XML document containing root element Cart and its element Total, TOTAL element is the total amount of the shopping cart, its initial value is 0, its XML specific form is as follows:
XML Version = '1.0' encoding = 'GB2312'?>
Public void emptycart () throws oException, saxexception {string stringcart = " xml version = '1.0'Encoding =' gb2312 '?>" "
public void addItemToCart (String stringItem) throws IOException, SAXException {// convert the item to a String XMLDocument XMLDocument itemAdded = parseString (stringItem); // item node extracted, and copy it NodeList itemList = itemAdded.getElementsByTagName ( "item"); Node itemlist.item (0); node cloneItem = item.clonenode (true); // If the shopping cart is empty, construct a new shopping cart IF (iscartempty ()) {mycart.emptycart ();} / / If the item is not in the shopping cart, insert the item and calculate the total amount if (! Isitemexist (item, mycart)) {// Take MyCart's root elements and add a copy of the ITEM node to the back Element Cartroot = MyCart .getdocumentelement (); node cartnode = cartroot.Appendchild (CloneItem); ComputeTAl (); // Calculate total amount}} 3. Deleting a product delete item, that is, deletes the item of the item, remove the item of the item from MyCart's root element Cart, and recalculate the value of Total: public void moveItemFromCart (String ID) {// Remove the node set in item-oriented Cartlist and root elements Cartroot nodelist cartlist = mycart.getLementsBytagname ("item"); element cartroot = mycart.getdocumentElement (); // Find code in CartList is the product for select ID (int x = 0; x public void addQuantityToCart (String qnty) throws IOException, SAXException {// will pass over a set of XML string containing the number of items to convert an XML document XMLDocument quantityChanged = parseString (qnty); // set quantity extraction node and comprising a number of new myCart quantity of the set of nodes NodeList quantityList = quantityChanged.getElementsByTagName ( "quantity"); NodeList cartList = myCart.getElementsByTagName ( "quantity"); // cyclically changing the number of items for (int x = 0; x Public boolean iscartempty () {// Item node set, if the number of nodes contains 0, there is no item in the shopping cart, return true nodelist itemlist = mycart.getlementsBytagname ("item"); if (itemlist.getlength); if (itemList.getlength); IF (itemList.getlength () == 0) Return True; Else Return False;} 7. It is judge whether or not the selected item has been judged in the shopping cart, if there is a new product's Item already exists in MyCart, if there is, return TRUE.