Select to view content in your preferred language

import text file into xml or datagrid

660
1
09-16-2010 02:59 PM
SebastianRoberts
Frequent Contributor
Hi,
  I am using the batch geocode sample found at
http://resources.esri.com/arcgisserver/apis/flex/index.cfm?fa=codeGalleryDetails&scriptID=16855

The widget loads a list of addresses from an xml file.  How could I have a user either paste a comma separated list of addresses into a textArea, or import a text file from a user?  Perhaps there is another better solution, but basically I need to get a list of addresses like
"123 South Street"
"345 Hampton Street"

into an xml file, or into my data grid.
Thanks,
Sebastian Roberts
Nevada County, CA GIS
Tags (2)
0 Kudos
1 Reply
SebastianRoberts
Frequent Contributor
I was able to find a solution for this.  I changed the dataProvider from the sample to point instead to an ArrayCollection.  Then the user can upload a text file with one address on each line.  The textfile is read into the the ArrayCollection which automatically updates the DataGrid.





import flash.events.IOErrorEvent;
import mx.collections.*;
                                      import flash.events.Event;
   import flash.net.FileFilter;
   import flash.net.FileReference;
   import flash.utils.ByteArray;
   private var fileRef:FileReference
   //File types which we want the user to open
   private static const FILE_TYPES:Array = [new FileFilter("Text File", "*.txt;*.text")];


private function doLoad():void{
                                                   fileRef = new FileReference();
    //listen for when they select a file
    fileRef.addEventListener(Event.SELECT, onFileSelect);
    
    //listen for when then cancel out of the browse dialog
    fileRef.addEventListener(Event.CANCEL,onCancel);
    
    //open a native browse dialog that filters for text files
    fileRef.browse(FILE_TYPES);
    
   }
   
   /************ Browse Event Handlers **************/
   
   //called when the user selects a file from the browse dialog
   private function onFileSelect(e:Event):void
   {
    //listen for when the file has loaded
    fileRef.addEventListener(Event.COMPLETE, onLoadComplete);
    
    //listen for any errors reading the file
    fileRef.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
    
    //load the content of the file
    fileRef.load();
   }
   
   //called when the user cancels out of the browser dialog
   private function onCancel(e:Event):void
   {
    trace("File Browse Canceled");
    fileRef = null;
   }
   
   /************ Select Event Handlers **************/
   
   //called when the file has completed loading
   private function onLoadComplete(e:Event):void
   {
    //get the data from the file as a ByteArray
    var data:ByteArray = fileRef.data;
    var result:String = new String(fileRef.data);
    var record:Array = new Array();
    var obj:Object;
    record = result.split("\r\n");
    for (var i:int = 0; i< record.length; i++){
     obj = new Object();
     obj.street = record;
     addressDataImported.addItem(obj);
    }
//    addressDataImported.addItem({street:"test street"});
    //read the bytes of the file as a string and put it in the
    //textarea
    //outputField.text = data.readUTFBytes(data.bytesAvailable);
    
    //clean up the FileReference instance
    
    fileRef = null;
   }
   
   //called if an error occurs while loading the file contents
   private function onLoadError(e:IOErrorEvent):void
   {
    trace("Error loading file : " + e.text);
   }





And here is the dataGrid element

<mx:DataGrid id="dg1" 
       dataProvider="{addressDataImported}" scroll="true" width="100%" height="150" 
       itemClick="onItemClick(event)"
       itemRollOver="onItemRollOver(event)"
       itemRollOut="onItemRollOut(event)">
    <mx:columns>
     <mx:DataGridColumn dataField="street" headerText="Street" width="55"/>
    </mx:columns>
   </mx:DataGrid>
0 Kudos