1

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);
7
  • 1
    Sorry, I'm feeling rather case-sensitive today. Commented May 20, 2011 at 22:26
  • dupe of stackoverflow.com/questions/4200913/xml-to-javascript-object Commented May 20, 2011 at 22:44
  • @Adam Bergmark i am not using JSON in this example Commented May 20, 2011 at 22:46
  • What do you mean by "associative array" then? The term doesn't exist in js. Commented May 20, 2011 at 22:48
  • @Adam Bergmark ok.i have used associative array and ksort for this example in php , which works correctly but now i want to do this in javascript... Commented May 20, 2011 at 22:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.