Jump to content

displaying specific XML object


robox45

Recommended Posts

Hello, this might be the most stupid thing a man can ask here, but i haven't used javascript and xml for couple of years and i have forgotten everything.

 

for example, Lets say i have a XML document called "phones.xml"

 

in this document, i have this information:

<phones>	<galaxy_s4>				<galaxy_s4_price>300 Eur</galaxy_s4_price>		<galaxy_s4_price_per_month>12 Eur</galaxy_s4_price_per_month>			</galaxy_s4>	<lg_g2>				<lg_g2_price>300 Eur</lg_g2_price>		<lg_g2_price_per_month>12 Eur</lg_g2_price_per_month>			</lg_g2>	<htc_one>				<htc_one_price>300 Eur</htc_one_price>		<htc_one_price_per_month>12 Eur</htc_one_price_per_month>			</htc_one>	<meizu_mx4>				<meizu_mx4_price>300 Eur</meizu_mx4_price>		<meizu_mx4_price_per_month>12 Eur</meizu_mx4_price_per_month>			</meizu_mx4></phones>

now, i want to display the price and price_per_month in my text just for the galaxy s4, but i don't want to put this information as a table, that shows all prices. i want just to print out for galaxy s4. I want to use this information in my text.

 

now, i tried to make an HTML document, that would show the XML information, but it doesnt work.

<head></head><body><script>if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.open("GET","phones.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML;var galaxy_s4 = xmlDoc.getElementsByTagName("galaxy_s4");var galaxy_s4_price = xmlDoc.getElementsByTagName("galaxy_s4_price")[0];var galaxy_s4_ppm = xmlDoc.getElementsByTagName("galaxy_s4_price_per_month")[0];document.write('for our latest, offer, the galaxy s4 costs only ');document.write(galaxy_s4_price);document.write(' and the price per month is ');document.write(galaxy_s4_ppm);</script></body>

when i try to run this code, instead of showing the price, it just prints out "undefined". how can i make the code to run and show my my information?

Edited by robox45
Link to comment
Share on other sites

        <script>            if (window.XMLHttpRequest)            {                xmlhttp = new XMLHttpRequest();            }            else            {                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");            }            xmlhttp.open("GET", "phones.xml", false);            xmlhttp.send();            xmlDoc = xmlhttp.responseXML;            var galaxy_s4 = xmlDoc.getElementsByTagName("galaxy_s4"); //parent node                        var galaxy_s4_price = galaxy_s4[0].getElementsByTagName("galaxy_s4_price")[0].childNodes[0].nodeValue; // parent node -> childnode (galaxy_s4_price)-this child node (text) and its value            var galaxy_s4_ppm = galaxy_s4[0].getElementsByTagName("galaxy_s4_price_per_month")[0].childNodes[0].nodeValue;// parent node -> childnode (galaxy_s4_price)-this child node (text) and its value            document.write('for our latest, offer, the galaxy s4 costs only ');            document.write(galaxy_s4_price);            document.write(' and the price per month is ');            document.write(galaxy_s4_ppm);        </script>
Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...