applying js function to multiple values in a .txt file

Discussion in 'JavaScript' started by motheherder, Nov 8, 2006.

  1. #1
    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
     
    motheherder, Nov 8, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    This would be easier with XML. Could you may post an example XML file if you can convert it?
     
    nico_swd, Nov 8, 2006 IP
  3. motheherder

    motheherder Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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):
     
    motheherder, Nov 8, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    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_swd, Nov 8, 2006 IP
  5. motheherder

    motheherder Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    nico

    thank you very much for help on this

    regards

    mo
     
    motheherder, Nov 8, 2006 IP
  6. motheherder

    motheherder Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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
     
    motheherder, Nov 10, 2006 IP