The realization of the shopping cart based on XML

Source: Internet
Author: User
Tags add contains empty return string version
xml| Shopping Cart

Shopping cart is an indispensable part of E-commerce site, but at present, most shopping carts can only be displayed as a customer selected goods, the client can not extract the contents of the cart to meet the needs of their own transaction processing, and this is necessary in some e-commerce activities. The advent of XML makes sense of the data transmitted over the network, and we can display the contents of a shopping cart in different styles according to different requirements.

This article will detail an xml-based shopping cart implemented by Java. Here is an XML inner structure of a shopping cart that contains five items: its root element is the Cart,total element that represents the total amount in the shopping cart, each item element represents a commodity, and the child element in the item identifies the specific information of the item, which can be added, modified or deleted according to the actual situation.

Here, you need to create a class that represents a shopping cart: Xmlcart.java, which is a javabean, so it contains an empty constructor. This class contains some basic features of the shopping cart: Generate an empty shopping cart, add items to the cart, delete the items in the cart, change the number of items in the cart, and empty the cart. It has a global private variable "private XmlDocument Mycart", Mycart to store the details of the cart, the basic function of the shopping cart is its operation, its type is XmlDocument, that is, an XML document. In this way, the operation of the shopping cart is converted into the addition, deletion of the child elements in the Mycart, and the calculation and modification of the element values.

1. Empty shopping Cart

Empty the shopping cart to generate an empty shopping cart. Here the empty shopping cart is an XML document containing the root element cart and its element total, which is an initial value of 0, and its XML is in the following form:

< XML version= ' 1.0 ' encoding= ' gb2312 '?>
< cart>
< total>0</total>
</cart>
Converts this XML string from the ParseString function into XmlDocument into the 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, the incoming item element will be added to the root element cart,
Where item contains details of the product,
Calculates the value of the total at the same time. The code is as follows:
public void Additemtocart (String stringitem)
Throws ioexception,saxexception{
Convert item from string to XmlDocument
XmlDocument itemadded=parsestring (Stringitem);
Remove 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, construct a new shopping cart
if (Iscartempty ()) {
Mycart.emptycart ();
}
If the product is not in the shopping cart, insert the item and calculate the total amount
if (!isitemexist (Item,mycart)) {
Takes the root element of the Mycart and adds the copied item node to the back
Element cartroot=mycart.getdocumentelement ();
Node Cartnode=cartroot.appendchild (Cloneitem);
Computetotal (); Calculate Total Amount
}
}
3. Delete Merchandise
Delete a product, that is, the item element of the item according to the commodity code
Removed from the root element cart of Mycart,
And recalculate the total value:
public void Moveitemfromcart (String id) {
Remove the node set Cartlist with item and the root element cartroot
NodeList cartlist=mycart.getelementsbytagname ("item");
Element cartroot=mycart.getdocumentelement ();
Look for items in cartlist that have code as a check ID
for (int x=0;x< cartlist.getlength (); x + +) {
Node Itemnode=cartlist.item (x);
String Idvalue=itemnode.getfirstchild ().
Getfirstchild (). Getnodevalue ();
If found, remove the node from the Cartroot and jump out of the loop
if (Idvalue.equals (ID)) {
Itemnode=cartroot.removechild (Itemnode);
Break
}
}
Computetotal (); Calculate Total Amount
}
4. Change the quantity of goods
According to the number of customers on the page, modify Mycart quantity,
And recalculate total:
public void Addquantitytocart (String qnty) throws
ioexception,saxexception{
Converts a collection of XML strings that contain the number of items to an XML document
XmlDocument quantitychanged=parsestring (Qnty);
Remove the quantity node set containing the new number and the quantity node set in Mycart
NodeList quantitylist=quantitychanged.getelementsbytagname ("Quantity");
NodeList cartlist=mycart.getelementsbytagname ("Quantity");
To change the number of items in a cycle
for (int x=0;x< cartlist.getlength (); x + +) {
Assign the value of the new quantity to the corresponding quantity in the Mycart
String Quantity=quantitylist.item (x). Getfirstchild (). Getnodevalue ();
Cartlist.item (x). Getfirstchild (). setNodeValue (quantity);
}
Computetotal (); Calculate Total Amount
}
5. Calculate Total Amount
That is, the value of total is computed, where Total=∑ (price*quantity):
public void Computetotal () {
NodeList quantitylist=mycart.getelementsbytagname ("Quantity");
NodeList pricelist=mycart.getelementsbytagname ("price");
float total=0;
Cumulative 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;
}
Attach total to total of Mycart
String totalstring=string.valueof (total);
Mycart.getelementsbytagname ("Total").
Item (0). Getfirstchild (). setNodeValue (totalstring);
}
6. Determine if the shopping cart is empty
Usually when you add a new product, you also need to know if the cart is empty,
If it is empty, a new shopping cart will be generated.
public Boolean iscartempty () {
The node set of item, if the node set contains 0 nodes, no merchandise in the shopping cart, returns true
NodeList itemlist=mycart.getelementsbytagname ("item");
if (Itemlist.getlength () ==0) return true;
else return false;
}
7. Determine if the selected item is already in the shopping cart
That is, to determine whether the item of the new item has already existed in the Mycart, and returns True if it exists.
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 above methods, Xmlcart also includes a method for converting an XML string into XmlDocument by a string that is entered, and a string of XML strings that is assigned to Mycart for output. Cartturntostringwithxsl method to assist the main operation of the shopping cart implementation, here no longer repeat.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.