Shopping cart is an indispensable part of e-commerce websites. However, currently, 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.
Shopping cart is an indispensable part of e-commerce websites. However, currently, 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 sports. XML rendering makes the data transmitted over the network meaningful. we can display the content of a shopping cart in different styles according to different requests.
This article will analyze an XML-based shopping cart implemented by Java. 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 product, 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 structure function. This class includes some basic functions of the shopping cart: a naturally empty shopping cart, adding items to the shopping cart, deleting the items in the shopping cart, changing the number of items in the shopping cart and clearing the shopping cart. It has a global private variable "private XMLDocument myCart". myCart is used to store the specific content in the shopping cart. The basic function of the shopping cart is to control it. its type is XMLDocument, it is an XML document. In this way, the control of the shopping cart is converted into the addition and deletion of child elements in myCart, and the calculation and correction of element values.
1. clear the shopping cart
Empty shopping cart is an empty shopping cart. Here, 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. 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 store it in 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 be uploaded to the cart of the root element. the item contains the specific information of the product and calculates the value of total. The code is as follows:
Public void addItemToCart (String stringItem)
Throws IOException, SAXException {
// Convert an item from String to XMLDocument
XMLDocument itemAdded = parseString (stringItem );
// Retrieve the item node and copy it
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 is structured.
If (isCartEmpty ()){
MyCart. emptyCart ();
}
// If the item is not in the shopping cart, insert it into 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 end
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 recalculate the value of total:
Public void moveItemFromCart (String id ){
// Extracts the cartList and root element cartRoot of the node set in item.
NodeList cartList = myCart. getElementsByTagName ("item ");
Element cartRoot = myCart. getDocumentElement ();
// In cartList, find the product with the selected id
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 jumps out of the loop.
If (idValue. equals (id )){
ItemNode = cartRoot. removeChild (itemNode );
Break;
}
}
ComputeTotal (); // calculates the total amount.
}
4. change the number of commodities
Modify the quantity in myCart according to the number entered by the customer on the page, and calculate total again:
Public void addQuantityToCart (String qnty) throws
IOException, SAXException {
// Convert the passed XML string containing the number of items to an XML document
XMLDocument quantityChanged = parseString (qnty );
// Retrieve the quantity node set that contains the new number of target quantity nodes and myCart.
NodeList quantityList = quantityChanged. getElementsByTagName ("quantity ");
NodeList cartList = myCart. getElementsByTagName ("quantity ");
// Number of items converted cyclically
For (int x = 0; x <cartList. getLength (); x ){
// Assign the value of the new quantity to the corresponding quantity in myCart
String quantity = quantityList. item (x). getFirstChild (). getNodeValue ();
CartList. item (x). getFirstChild (). setNodeValue (quantity );
}
ComputeTotal (); // calculates the total amount.
}
5. calculate the total amount
That is, 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;
// Accumulate the total 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 total of myCart
String totalString = String. 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 empty, you need to create a new shopping cart.
Public boolean isCartEmpty (){
// Item node set. if the number of nodes contained in the node set is 0, no items are in the shopping cart and true is returned.
NodeList itemList = myCart. getElementsByTagName ("item ");
If (itemList. getLength () = 0) return true;
Else return false;
}
7. determine whether the selected item is in the shopping cart
Determines 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 );
7 Node 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 contains the parseString method for converting the XML String from the input String to the XMLDocument, and the cartTurnToStringWithXSL method that is used to assign XSL to myCart during output and return the String-type XML String to help implement the important control of the shopping cart.