Jump to content

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;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.