Implementation of xml-based shopping cart with Java

Source: Internet
Author: User
Tags empty

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 is added to the root element cart, where item contains the details of the item and the total value is calculated. The code is as follows:

public void addItemToCart(String stringItem)
throws IOException,SAXException{
 //将item由String转换为XMLDocument
 XMLDocument itemAdded=parseString(stringItem);
 //取出item节点,并复制它
 NodeList itemList=itemAdded.getElementsByTagName(“item”);
 Node item=itemList.item(0);
 Node cloneItem=item.cloneNode(true);
 //如果购物车为空,则构造一个新的购物车
 if(isCartEmpty()){
  myCart.emptyCart();
 }
 //如果该商品不在购物车中,则插入该商品,并计算总金额
 if(!isItemExist(item,myCart)){
  //取myCart的根元素,并将复制的item节点添加到后面
  Element cartRoot=myCart.getDocumentElement();
  Node cartNode=cartRoot.appendChild(cloneItem);
  computeTotal(); //计算总金额
 }
}

Related Article

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.