mentalist Posted May 29, 2011 Share Posted May 29, 2011 Hi, I've found a few examples and references which all state this should work: <script> s=' \ <fruits> \ <fruit name="Apple" colour="Green" /> \ <fruit name="Banana" colour="Yellow" /> \ </fruits> \ '; function gen_xml(s){ if (window.DOMParser) { parser=new DOMParser(); xmlDoc=parser.parseFromString(s,"text/xml"); } else{ // Internet Explorer xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.loadXML(s); } return xmlDoc; } function getFruits(xml){ var fruits = xml.getElementsByTagName("fruits")[0]; if (fruits) { var fruitsNodes = fruits.childNodes; if (fruitsNodes){ for (var i = 0; i < fruitsNodes.length; i++){ var name = fruitsNodes[i].getAttribute("name"); var colour = fruitsNodes[i].getAttribute("colour"); alert("Fruit " + name + " is coloured " + colour); } } } } xml=gen_xml(s); getFruits(xml); </script> But I keep getting the following error: Error: fruitsNodes[i].getAttribute is not a function Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/237811-parsing-xml/ Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 add this line at the start of your for loop: if(fruitsNodes[i].nodeType!=1) continue; HTH Quote Link to comment https://forums.phpfreaks.com/topic/237811-parsing-xml/#findComment-1222123 Share on other sites More sharing options...
mentalist Posted May 30, 2011 Author Share Posted May 30, 2011 Thankyou, that's a great help and shed some light as to why. I'd used this way by the end of last night (I'll add just for examples): fruitsNodes=xml.getElementsByTagName('fruit'); for (i=0;i<fruitsNodes.length;i++){ var name = fruitsNodes[i].getAttribute("name"); var colour = fruitsNodes[i].getAttribute("colour"); alert("Fruit " + name + " is coloured " + colour); } Slight changes, yet basically the same, but it's just the loop type from what I can see... Quote Link to comment https://forums.phpfreaks.com/topic/237811-parsing-xml/#findComment-1222290 Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 It is the same type of loop, but you are basically looping over a different set of elements. In your original, you were looping over all the childrenNodes of xml.getElementsByTagName("fruits")[0], which included text nodes and other what-nots, including children elements without <fruit> tags. If I have a <citrous> element under <fruits>, the original piece of code will also be able to access the <citrous> element. In your new piece of code, you are specifically looking for elements with the tag "fruit". This is probably faster if you simply want to find only elements with the <fruit> tag. PS: Do remember to mark the topic as "Solved" too! Quote Link to comment https://forums.phpfreaks.com/topic/237811-parsing-xml/#findComment-1222296 Share on other sites More sharing options...
mentalist Posted May 30, 2011 Author Share Posted May 30, 2011 Cheers! I'd investigated, checking for what was there using: var o; for(o in fruitsNodes[i].childNodes){ try { desc = fruitsNodes[i].childNodes[o]; } catch(e) { desc = "ERROR"; } alert("o: " + o + ", desc: " + desc); } I'd say it's a bit like Python's 'dir' function for javascript. Quote Link to comment https://forums.phpfreaks.com/topic/237811-parsing-xml/#findComment-1222312 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.