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.
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/> " + 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/> 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/> " + itemDescription + " " + itemPrice);
}
}
document.write("<br/>Total = " + totalPrice + "<br/><br/>");
}
}
</script>