473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to fetch data from XML file into combo-boxes using javascript based on DOM API

I have XML file with name "mobiles.xml"; The XML file looks like;

Expand|Select|Wrap|Line Numbers
  1. <mobiles>
  2.  
  3. <brand name="Nokia">
  4. <model>Lumia 720</model>
  5. <price>Rs. 27,600</price>
  6.  
  7. <model>Lumia 510</model>
  8. <price>Rs. 15,300</price>
  9.  
  10. <model>Asha 303</model>
  11. <price>Rs. 12,900</price>
  12.  
  13. <model>X2 05</model>
  14. <price>Rs. 5,550</price>
  15. </brand>
  16.  
  17. <brand name="Samsung">
  18. <model>Galaxy Mega 5.8</model>
  19. <price>Rs. 42,000</price>
  20.  
  21. <model>S5610</model>
  22. <price>Rs. 10,000</price>
  23.  
  24. <model>E1207</model>
  25. <price>Rs. 2,000</price>
  26.  
  27. <model>E1205</model>
  28. <price>Rs. 1,700</price>
  29. </brand>
  30.  
  31. <brand name="Sony">
  32. <model>Xperia ZL</model>
  33. <price>48,000</price>
  34.  
  35. <model>Xperia J</model>
  36. <price>Rs. 18,000</price>
  37.  
  38. <model>Xperia miro</model>
  39. <price>Rs. 16,000</price>
  40.  
  41. <model>Xperia Tipo Dual</model>
  42. <price>Rs. 12,000</price>
  43. </brand>
  44.  
  45. </mobiles>
Actually I want to retrieve data from XML file through javascript which is DOM based (node based) but I don't know how to do it. I want to retrieve data from XML file into combo-boxes (dropdown lists)... For example, I have to fetch the data of "brand" from XML file in combo-box. When i select the particular brand the values of "models" should be loaded from XML file into another combo-box. Now I have 3rd element price in XML File. I want to display the price in textbox of a particular mobile based on selected values of combo-boxes "Brand" & "Models" after hitting Submit button. How will I do this? Please don't use built-in functions. I want to learn how to do it based on DOM API and Javascript. It will be nice if you people also tell me how to add and save data of new brands, their models and price through textboxes into XML file? I have worked on it and I can load values in combo-boxes but I want to display the price of particular item based on combo-box selection in textbox.


I have fetch the data in 2 combo-boxes from XML file and code looks like;
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <script language="javascript">
  5. /* Cross-Browser Import of XML document into a XML Document. */
  6. function importXML(xmlfile) {
  7.     var xmlDoc;
  8.     if (typeof window.ActiveXObject != 'undefined') {
  9.         //code for IE
  10.         xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  11.         xmlDoc.async = false;
  12.         xmlDoc.load(xmlfile);
  13.         } else {
  14.             try {
  15.                 // code for IE7+, Firefox, Chrome, Opera, Safari
  16.                 xmlhttp=new XMLHttpRequest();
  17.                 xmlhttp.open("GET", xmlfile, false);
  18.                 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
  19.                 xmlhttp.send();
  20.                 xmlDoc=xmlhttp.responseXML;
  21.             } catch (Exception) {
  22.                 alert("Your browser is not supported. Try firefox !!");
  23.             }
  24.         }
  25.     return xmlDoc;
  26. }
  27.  
  28. /* Get the options for the model combobox from the XML. */
  29. function loadXMLOption() {
  30.     // Load the xml file
  31.     var xmlDoc=importXML("mobiles.xml");
  32.     xmlObj=xmlDoc.documentElement;
  33.     // Get all Elements with Tag name 'brand'
  34.     BrandObj = xmlObj.getElementsByTagName("brand");
  35.     for(var i=0; i < BrandObj.length; i++) {
  36.         // Loop through each brand tag and check if its name is equal to the selected brand value
  37.         if (document.getElementById('Company').value == BrandObj[i].getAttribute("name")) {  
  38.             // If matching brand found get the model tags under that brand
  39.             modelObj = BrandObj[i].getElementsByTagName("model");
  40.             document.getElementById('Model').options.length = 0;
  41.             // Create options for the Model comboBox.
  42.             for(var j=0; j < modelObj.length; j++) {
  43.                 var opt = document.createElement('option');
  44.                 opt.value = modelObj[j].firstChild.nodeValue;
  45.                 opt.text = modelObj[j].firstChild.nodeValue;
  46.                 document.getElementById('Model').options.add(opt);      }     }     } }
  47. </script>
  48. </head>
  49. <body>
  50. <h2>Chained Drop-Down - using XML</h2>
  51. Brand : <select id="Company" onchange="loadXMLOption();">
  52.   <option value=""></option> 
  53.   <option value="Nokia">Nokia</option>
  54.   <option value="Samsung">Samsung</option> 
  55.   <option value="Sony">Sony</option>
  56. </select>
  57. Model : <select id="Model"> </select>
  58. </body></html>
Now I want to get the value of price against particular item selected in comboboxes in textbox. How will I do it?
Jan 7 '14 #1
0 1748

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Sergio Otoya | last post by:
Hi all, Is there any way of copying a file using javascript, not using the Filesystemobject (ActiveX). I need this to run in Windows and MACS. Any help would be greatly appreciated. Thanks...
1
by: guitarsajee | last post by:
hi.. i want to know how to validate file tag using javascript... plz let me know soon as possible.. thanx...
6
by: mitchell | last post by:
hi i was trying to delete a file in a javascript function but couldn't do it. myActiveXObject = new ActiveXObject("Scripting.FileSystemObject"); file =...
2
by: ranjiths | last post by:
hi, i am trying to export data to excel from html using javascript, but i am getting an error as it opens saveas window. Please do the needful
3
Frinavale
by: Frinavale | last post by:
Hi there! I'm hoping that someone knows how to check the size of a file before it is uploaded to the server using JavaScript. I have seen suggested solutions using an ActiveX control to check...
1
by: rahullko05 | last post by:
Hi, i am building a small forum site as my final year project & stuck in a very trivial problem. I have a table which are varchar type of data & i am trying to fetch data from table based on...
0
by: Aditya jha | last post by:
Hi, I want to fetch data from DB2 using INNER JOIN with access file, Is it possible. If yes then how?.
2
by: satishpatil14 | last post by:
how to store server fetched data in array using JavaScript and HTML? please give reply to this.Thanks in Advance
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.