Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want to save shopping cart as XML object

Status
Not open for further replies.

JCGrafted

Technical User
Nov 10, 2009
4
I am writing a store from scratch. I was using cookies. Worked great for mini-cart but found it not flexible enough for cart. I am trying to use XML instead. I am an XML newbie.

The following works in IE but nowhere else.
Code:
<script language="javascript" type="text/javascript">

function init()
{
  var xmlDoc;
  //code for IE
  if (window.ActiveXObject)
  {
    xmlDoc = new ActiveXObject("MSXML2.DOMDocument"); //("Microsoft.XMLDOM")
  }
  //code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation.createDocument)
  {
    xmlDoc = document.implementation.createDocument("","",null);
  }
  else
  {
    alert('Your browser cannot handle this script');
  }
  xmlDoc.async = false;
  xmlDoc.loadXML("<root/>");


  var rootNode = xmlDoc.documentElement;

  //Add member and set attributes
  var memberNode = xmlDoc.createElement("member");
  rootNode.appendChild(memberNode);
  memberNode.setAttribute("id", "1");
  memberNode.setAttribute("name", "george");

  //Add item and set attributes
  var itemNode = xmlDoc.createElement("item");
  memberNode.appendChild(itemNode);
  itemNode.setAttribute("id", "1");
  itemNode.setAttribute("code", "1");
  itemNode.setAttribute("description", "Conference Full");
  itemNode.setAttribute("price", "150");

  //Add guest and set attributes
  var guestNode = xmlDoc.createElement("guest");
  memberNode.appendChild(guestNode);
  guestNode.setAttribute("id", "1");
  guestNode.setAttribute("name", "sally");

  //Add guest item and set attributes
  itemNode = xmlDoc.createElement("item");
  guestNode.appendChild(itemNode);
  itemNode.setAttribute("id", "5");
  itemNode.setAttribute("code", "7");
  itemNode.setAttribute("description", "T-shirt M");
  itemNode.setAttribute("price", "10");

  
  //Show xml contents
  var memberNodeList = rootNode.getElementsByTagName("member");
  for (var i=0;i<memberNodeList.length;i++)
  {
    var totalPrice = 0;
    var memberName = memberNodeList[i].getAttribute("name");
    document.write("Member: " + name);
    var itemNodeList = memberNodeList[i].getElementsByTagName("item");
    for (var j=0;j<itemNodeList.length;j++)
    {
      var itemDescription = itemNodeList[j].getAttribute("description");
      var itemPrice = itemNodeList[j].getAttribute("price");
      totalPrice += parseFloat(itemPrice);
      document.write("<br/>&nbsp;&nbsp;" + itemDescription + " " + itemPrice);
    }
    var guestNodeList = memberNodeList[i].getElementsByTagName("guest");
    for (var j=0;j<guestNodeList.length;j++)
    {
      var guestName = guestNodeList[j].getAttribute("name");
      document.write("<br/>&nbsp;&nbsp;Guest: " + guestName);
      itemNodeList = guestNodeList[i].getElementsByTagName("item");
      for (var k=0;k<itemNodeList.length;k++)
      {
        var itemDescription = itemNodeList[k].getAttribute("description");
        var itemPrice = itemNodeList[k].getAttribute("price");
        totalPrice += parseFloat(itemPrice);
        document.write("<br/>&nbsp;&nbsp;&nbsp;&nbsp;" + itemDescription + " " + itemPrice);
      }
    }
    document.write("<br/>Total = " + totalPrice + "<br/><br/>");
  }
}
</script>

 
Well, I see this as a Javascript problem. What do you mean by "not work"?

Cheers,
Dian
 
In IE the page displays the following:

Member:
Conference Full 150
T-shirt M 10
Guest: sally
T-shirt M 10
Total = 170

In all others it displays nothing.

This is just a basic start. I want to make sure I can create an XML object and that I can programmatically add, remove, and display from it. Then I will plug in the code that before wrote to the cookie.

Jonathan
 
I got it!

I am pasting what worked in case anyone else is having a similar problem.

If anyone has a better solution I would still like to hear it.

Code:
<script language="javascript" type="text/javascript">

var text = "";
var xmlDoc;

function init()
{
  //code for IE
  if (window.ActiveXObject)
  {
    xmlDoc = new ActiveXObject("MSXML2.DOMDocument"); //("Microsoft.XMLDOM")
    xmlDoc.async = false;
    xmlDoc.loadXML("<root/>");
  }
  //code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation && document.implementation.createDocument)
  {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(text,"text/xml");
  }
  else
  {
    alert('Your browser cannot handle this script');
  }


  var rootNode = xmlDoc.documentElement;

  //Add member and set attributes
  var memberNode = xmlDoc.createElement("member");
  rootNode.appendChild(memberNode);
  memberNode.setAttribute("id", "1");
  memberNode.setAttribute("name", "george");

  //Add item and set attributes
  var itemNode = xmlDoc.createElement("item");
  memberNode.appendChild(itemNode);
  itemNode.setAttribute("id", "1");
  itemNode.setAttribute("code", "1");
  itemNode.setAttribute("description", "Conference Full");
  itemNode.setAttribute("price", "150");

  //Add guest and set attributes
  var guestNode = xmlDoc.createElement("guest");
  memberNode.appendChild(guestNode);
  guestNode.setAttribute("id", "1");
  guestNode.setAttribute("name", "sally");

  //Add guest item and set attributes
  itemNode = xmlDoc.createElement("item");
  guestNode.appendChild(itemNode);
  itemNode.setAttribute("id", "5");
  itemNode.setAttribute("code", "7");
  itemNode.setAttribute("description", "T-shirt M");
  itemNode.setAttribute("price", "10");

  //Show xml contents
  var memberNodeList = rootNode.getElementsByTagName("member");
  for (var i=0;i<memberNodeList.length;i++)
  {
    var totalPrice = 0;
    var memberName = memberNodeList[i].getAttribute("name");
    document.write("Member: " + name);
    var itemNodeList = memberNodeList[i].getElementsByTagName("item");
    for (var j=0;j<itemNodeList.length;j++)
    {
      var itemDescription = itemNodeList[j].getAttribute("description");
      var itemPrice = itemNodeList[j].getAttribute("price");
      totalPrice += parseFloat(itemPrice);
      document.write("<br/>&nbsp;&nbsp;" + itemDescription + " " + itemPrice);
    }
    var guestNodeList = memberNodeList[i].getElementsByTagName("guest");
    for (var j=0;j<guestNodeList.length;j++)
    {
      var guestName = guestNodeList[j].getAttribute("name");
      document.write("<br/>&nbsp;&nbsp;Guest: " + guestName);
      itemNodeList = guestNodeList[i].getElementsByTagName("item");
      for (var k=0;k<itemNodeList.length;k++)
      {
        var itemDescription = itemNodeList[k].getAttribute("description");
        var itemPrice = itemNodeList[k].getAttribute("price");
        totalPrice += parseFloat(itemPrice);
        document.write("<br/>&nbsp;&nbsp;&nbsp;&nbsp;" + itemDescription + " " + itemPrice);
      }
    }
    document.write("<br/>Total = " + totalPrice + "<br/><br/>");
  }
}
</script>

Jonathan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top