Shopping cart is an indispensable part of e-commerce websites. However, most shopping cart can only be used as a display of the items selected by a customer, the client cannot extract the content in the shopping cart to meet the needs of transaction processing, which is necessary in some e-commerce activities. The emergence of xml makes the data transmitted over the network meaningful. we can display the content of a shopping cart in different styles according to different requirements. Shopping cart is an indispensable part of e-commerce websites. However, most shopping cart can only be used as a display of the items selected by a customer, the client cannot extract the content in the shopping cart to meet the needs of transaction processing, which is necessary in some e-commerce activities. The emergence of xml makes the data transmitted over the network meaningful. we can display the content of a shopping cart in different styles according to different requirements.
This article will detail a java-based XML-based shopping cart. The following is an XML internal structure of a shopping cart containing five items: its root element is cart, the total element represents the total amount in the shopping cart, and each item element represents a commodity, the sub-elements in item indicate the specific information of the item, and can be added, modified, or deleted according to the actual situation.
Here, you need to create a class that represents the shopping cart: XMLCart. java, which is a JavaBean, so it contains an empty constructor. This class includes some basic functions of the shopping cart: generate an empty shopping cart, add items to the shopping cart, delete the items in the shopping cart, change the quantity of items in the shopping cart, and clear the shopping cart. It has a global PRivate variable "PRivate XMLDocument myCart". myCart is used to store the details in the shopping cart. The basic function of the shopping cart is to operate on it. its type is XMLDocument, it is an XML document. In this way, the operations on the shopping cart are converted into the addition and deletion of child elements in myCart, and the calculation and modification of element values.
1. clear the shopping cart
Empty shopping cart generates an empty shopping cart. The empty shopping cart is an XML document containing the root element cart and its element total. The total element is the total amount of the shopping cart, and its initial value is 0. the XML format is as follows:
< ?xml version=‘1.0’ encoding=‘gb2312’?>< cart>< total>0< /total>< /cart>
Convert the XML string from the parseString function to XMLDocument and save it to myCart.
The code is as follows:
public void emptyCart() throws IOException,SAXException{ String stringCart=“< ?xml version=‘1.0’encoding=‘gb2312’?> ”+ “< cart>< total>0< /total>< /cart>”; myCart=parseString(stringCart); }
2. add product
Add the item to add the imported item element to the root element cart,
Item contains the product details,
Calculate the total value. The code is as follows:
Public void addItemToCart (String stringItem) throws IOException, SAXException {// Convert the item from String to XMLDocumentXMLDocument itemAdded = parseString (stringItem); // retrieve the item node, and copy it to NodeList itemList = itemAdded. getElementsByTagName ("item"); Node item = itemList. item (0); Node cloneItem = item. cloneNode (true); // if the shopping cart is empty, a new shopping cart, if (isCartEmpty () {myCart, is constructed. emptyCart ();} // if the item is not in the shopping cart, insert the item and calculate the total amount if (! IsItemExist (item, myCart) {// Obtain the root Element of myCart and add the copied item node to the following Element cartRoot = myCart. getDocumentElement (); Node cartNode = cartRoot. appendChild (cloneItem); computeTotal (); // calculates the total amount }}
3. delete a product
Delete the item.
Delete from the root element cart of myCart,
And re-calculate the total value:
Public void moveItemFromCart (String id) {// retrieves the cartList and cartRoot NodeList cartList = myCart. getElementsByTagName ("item"); Element cartRoot = myCart. getDocumentElement (); // in cartList, find the product for (int x = 0; x <cartList. getLength (); x ++) {Node itemNode = cartList. item (x); String idValue = itemNode. getFirstChild (). getFirstChild (). getNodeValue (); // if it is found, the node is deleted from cartRoot and the loop if (idValue. equals (id) {itemNode = cartRoot. removeChild (itemNode); break ;}} computeTotal (); // calculates the total amount}
4. change the product quantity
Modify the quantity in myCart according to the quantity entered by the customer on the page,
And recalculate total:
Public void addQuantityToCart (String qnty) throws IOException, SAXException {// convert a set of XML strings containing the number of items to the XML document XMLDocument quantityChanged = parseString (qnty ); // Retrieve the quantity node set NodeList quantityList = quantityChanged. getElementsByTagName ("quantity"); NodeList cartList = myCart. getElementsByTagName ("quantity"); // cyclically change the number of items for (int x = 0; x <cartList. getLength (); x ++) {// assign the new quantity value to the corresponding quantity in myCart to remove String quantity = quantityList. item (x ). getFirstChild (). getNodeValue (); cartList. item (x ). getFirstChild (). setNodeValue (quantity) ;}computetotal (); // calculates the total amount}
5. calculate the total amount
Calculate the value of total, where total = Σ (price * quantity ):
Public void computeTotal () {NodeList quantityList = myCart. getElementsByTagName ("quantity"); NodeList priceList = myCart. getElementsByTagName ("price"); float total = 0; // the total accumulated amount 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;} // attaches total to totalString = String of myCart. valueOf (total); myCart. getElementsByTagName ("total "). item (0 ). getFirstChild (). setNodeValue (totalString );}
6. check whether the shopping cart is empty.
When adding a new item, you also need to know whether the shopping cart is empty,
If it is null, a new shopping cart is generated.
Public boolean isCartEmpty () {// item's node set. if the number of nodes contained in this node set is 0, there is no item in the shopping cart, and trueNodeList itemList = myCart is returned. getElementsByTagName ("item"); if (itemList. getLength () = 0) return true; else return false ;}
7. determine whether the selected item is in the shopping cart
That is, to determine whether the item of the new product already exists in myCart. If yes, return 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; }
In addition to the preceding method, XMLCart also includes the parseString method for converting an XML String from the String at the input time to the XMLDocument, and the cartTurnToStringWithXSL method that is used to grant the XSL to myCart during output and return the String XML String to help implement the main operations of the shopping cart.
The above is the details of the instance code of the XML-based shopping cart. For more information, see PHP Chinese network (www.php1.cn )!