Mode about the purchase car in ASP.NET

xiaoxiao2021-03-06  176

http://community.9cbs.net/expert/topic/3170/3170136.xml?temp=.5326654

Http://community.9cbs.net/expert/topic/2901/2901250.xml?temp=.1860773

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. Clear shopping cart

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:

0 Transfer this XML string from the Parsestring function into Xmldocument to store Mycart. The code is as follows: Public void Emptycart () throws ioException, saxexception {string stringcart = "" " 0 "; Mycart = parseString (StringCart);

2. Add a product to add a product, the incoming Item element is added to the root element Cart, where the item includes the product details while calculating the value of Total.

Code is as follows: public void addItemToCart (String stringItem) throws IOException, SAXException {// convert the item to a String XMLDocumentXMLDocument itemAdded = parseString (stringItem); // item node extracted, and copy it NodeList itemList = itemAdded.getElementsByTagName ( "item "); Node item = 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 the root element of Mycart and add the copy ITEM node to the back Element Cartroot = mycart.getdocumentElement (); Node CartNode = Cartroot.Appendchild (CloneItem); ComputeTotal (); // Computing total amount}} 3. Deleting the product deletes the item, that is, delete the item of the item from MyCart's root element Cart according to the product code, and recalculate the value of Total: public void moveItemFromCart (String ID) {// Remove the node set with item in item to 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

Mycart.getElementsBytagname ("Quantity"); // Cycle changes the number of items for for (int x = 0; x

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

http://www.fawcette.com/china/print.aspx?totalpage=4&id=94

转载请注明原文地址:https://www.9cbs.com/read-128204.html

New Post(0)