hi this is probably more of a general question, I need to apply a function that converts xy coordinate values into lat long to a list of potentially hundreds of coordinates. the natural format of the coordinate values is .txt but it shouldn't be a problem to convert these to xml or some other format. how do I go about using JS to read in the txt file loop through and apply the function to each value in the file. i am still very much a JS novice so any pointers in the right direction, possibly some examples on reading in files and applying functions to their contents would be much appreciated mo
nico thank you for your response, i have included two possible xml format samples below [INDENT][/INDENT]<dataset label="roadwork locations"> <point> <coord axis="x" value="556612.99"/> <coord axis="y" value="160732.03"/> </point> <point> <coord axis="x" value="544313.85"/> <coord axis="y" value="162338.81"/> </point> <point> <coord axis="x" value="551222.99"/> <coord axis="y" value="172140.15"/> </point> <point> <coord axis="x" value="556715.24"/> <coord axis="y" value="162850.06"/> </point> <point> <coord axis="x" value="545555.45"/> <coord axis="y" value="160717.43"/> </point> <point> <coord axis="x" value="556583.78"/> <coord axis="y" value="164456.83"/> </point> <point> <coord axis="x" value="555459.03"/> <coord axis="y" value="172198.57"/> </point> <point> <coord axis="x" value="547395.94"/> <coord axis="y" value="160819.68"/> </point> </dataset> Code (markup): OR [INDENT][/INDENT]<dataset label="roadwork locations"> <point> <xcoord>556612.99</xcoord> <ycoord>160732.03</ycoord> </point> <point> <xcoord>544313.85</xcoord> <ycoord>162338.81</ycoord> </point> <point> <xcoord>551222.99</xcoord> <ycoord>172140.15</ycoord> </point> <point> <xcoord>546715.24</xcoord> <ycoord>162850.06</ycoord> </point> <point> <xcoord>555555.45</xcoord> <ycoord>160717.43</ycoord> </point> <point> <xcoord>546583.78</xcoord> <ycoord>164456.83</ycoord> </point> <point> <xcoord>555459.03</xcoord> <ycoord>172198.57</ycoord> </point> <point> <xcoord>547395.94</xcoord> <ycoord>160819.68</ycoord> </point> </dataset> Code (markup):
Here you go. (For the second XML file) <script type="text/javascript"> <!-- var xml_file = 'data.xml'; function importXML() { if (document.implementation && document.implementation.createDocument) { xmlDoc = document.implementation.createDocument("", "", null); xmlDoc.onload = loop_through; } else if (window.ActiveXObject) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.onreadystatechange = function () { if (xmlDoc.readyState == 4) { loop_through(); } } } else { alert('Your browser can\'t handle this script'); return; } xmlDoc.load(xml_file); } function loop_through() { var points = xmlDoc.getElementsByTagName('point'); for (var i in points) { var x = points[i].getElementsByTagName('xcoord')[0].firstChild.nodeValue; var y = points[i].getElementsByTagName('ycoord')[0].firstChild.nodeValue; // Do whatever you want with x and y here } } importXML(); //--> </script> Code (javascript):
nico or others the code nico kindly provided works perfectly. When alerting var x and var y from within the function loop_through() all x and y values from the xml are alerted as expected. I am trying to display these(list of x and y values) in the html body doing the following but cant seem to get it to work (undefined is desplayed several times), does anyone have any suggestions or pointers in the right direction? global var osArray = new Array() Code (markup): within function loop_through() osArray[i] = (x,y); Code (markup): in HTML body tag <script language="javascript"> for (i=0; i < 8; i++) { document.write(osArray[i]); document.write("<br>"); } </script> Code (markup): thank you mo