Jump to content

Cant seem to get XML node value..


chelnov63

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/175914-cant-seem-to-get-xml-node-value/
Share on other sites

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

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.