Reputation: 11
i want to select just two items from xml file with itemno and quantity that have less prices.can anybody help me how to do this in javascript.
items.xml:
<?xml version="1.0"?>
<items>
<item>
<itemno>1</itemno>
<unitprice>99</unitprice>
<Quantity>10</Quantity>
</item>
<item>
<itemno>2</itemno>
<unitprice>80</unitprice>
<Quantity>10</Quantity>
</item>
<item>
<itemno>3</itemno>
<unitprice>120</unitprice>
<Quantity>10</Quantity>
</item>
</items>
javascript:
var xmlDoc=new ActiveXObject("MSXML.DOMDocument");
xmlDoc.async="false";
xmlDoc.load("items.xml");
var items=xmlDoc.documentElement;
var item = itemss.childNodes(0);
Upvotes: 1
Views: 3265
Reputation: 69944
All of the basic DOM methods also work for XML:
http://www.quirksmode.org/dom/w3c_core.html
https://developer.mozilla.org/en/DOM/document
https://developer.mozilla.org/en/DOM/element
You can find all items using
items = xmlDoc.getElementsByTagName('item');
For each item, one of the children is the cost node:
priceNode = item.childNodes[1];
(Or you could go through the childnodes, looking for the one with nodeName
equal to "UNITPRICE")
Looking for the content of the node is trickier, since IE and FF support different methods:
priceStr = priceStr.textContent || priceNode.innerText;
Finally, to convert a string into a number:
price = parseInt(priceStr, 10);
By the way, your way of building a XML document with ActiveX is IE specific. You should definitely look into using some sort of Javascript library (such as Jquery or Dojo) to smooth this and other things out.
Upvotes: 1