chelnov63 Posted September 29, 2009 Share Posted September 29, 2009 hi everyone this is my XML which I am loading in with javascript: <?xml version="1.0" encoding="utf-8"?> <config> <config> <config id="annualMileage"><![CDATA[10000]]></config> <config id="petrolPrice"><![CDATA[97.5]]></config> <config id="thumbnailPath"><![CDATA[images/]]></config> </config> <content> <!-- data view --> <text id="saving_power"><![CDATA[{moreless} Power]]></text> <text id="saving_mileage"><![CDATA[{moreless} Efficiency]]></text> </content> </config> Now the XML loads succesfully into an xml object XMLDoc ... however I am trying to retrieve the value of petrolPrice ..but I can't seem to get the path to it correct: //by this point XMLDoc does contain the XML data var petrol_price = XMLDoc.getElementsByTagName("config")[0].childNodes[0].childNodes[1].firstChild.nodeValue; alert(petrol_price); Have I got the path wrong? thanks in advance for your help Quote Link to comment https://forums.phpfreaks.com/topic/175914-cant-seem-to-get-xml-node-value/ Share on other sites More sharing options...
broseph Posted September 29, 2009 Share Posted September 29, 2009 Well, for starters you seem to have two nodes named the same thing, perhaps that is confusing it? Quote Link to comment https://forums.phpfreaks.com/topic/175914-cant-seem-to-get-xml-node-value/#findComment-927039 Share on other sites More sharing options...
chelnov63 Posted September 30, 2009 Author Share Posted September 30, 2009 Thanks i would love to change it - but i dont have access to the XML file - and it wont change since it has other dependants .. do you think that is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/175914-cant-seem-to-get-xml-node-value/#findComment-927563 Share on other sites More sharing options...
broseph Posted September 30, 2009 Share Posted September 30, 2009 yes i really do think that is the problem. you have 3 nodes with identical names. <node> <node> <node/> <node/> </node> </node> look up the documentation for getElementsByTagName() and see what it says about what node it will give you a reference to when you have multiple nodes with the same name Quote Link to comment https://forums.phpfreaks.com/topic/175914-cant-seem-to-get-xml-node-value/#findComment-927815 Share on other sites More sharing options...
salathe Posted October 1, 2009 Share Posted October 1, 2009 Have I got the path wrong? thanks in advance for your help The whitespace nodes between the normal nodes (config, etc.) still count as nodes when you're traversing through the childNodes collections. The following (untested) should be what you're after: var petrol_price = XMLDoc.getElementsByTagName("config")[0].childNodes[1].childNodes[3].firstChild.nodeValue; Quote Link to comment https://forums.phpfreaks.com/topic/175914-cant-seem-to-get-xml-node-value/#findComment-928354 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.