KMLParser.java

Home

view pathCoords.kml file (copy and paste it into a file and name it pathCoords.kml)

This example parses a Google Earth KML file for coordinates.

   
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class KMLParser {
    
    
    public ArrayList parseKML(String strFile){
        
        ArrayList arrListCoords= new ArrayList();
        
        try
        {
            
            ArrayList arrListStrLine= new ArrayList();
           // ArrayList arrListLoc3D= new ArrayList();
           
            //create BufferedReader to read file
             BufferedReader br = new BufferedReader( new FileReader(strFile));
            String strLine = "";
            StringTokenizer st = null;
           
            boolean insideCoordTags = false;
             
            //read file line by line
            while( (strLine = br.readLine()) != null)
            {
                   
                st = new StringTokenizer(strLine, " ");
        
                while(st.hasMoreTokens())
                {
                    
                    String val = st.nextToken(); 
                   
                    val = val.replaceAll("\\s+", ""); // trim whitespaces
                     
                    if (val.equals(""))
                    {
                       insideCoordTags = true;
                    }
                    
                    if (val.equals(""))
                     {
                       insideCoordTags = false;
                    }
                    
                    if (insideCoordTags){
                        if(!val.equals("") )
                             arrListStrLine.add(val);                                    
                    }
                } // end inner while loop
                   
            } // end outer while loop
             
         
            
            for(int i = 0; i < arrListStrLine.size(); i++){
                
                String tempStr = arrListStrLine.get(i);
                 String[] splits = tempStr.split(",");
                
                if (splits.length == 3){
                double lat = Double.parseDouble(splits[0]);
                double lon = Double.parseDouble(splits[1]);
                 double elev = Double.parseDouble(splits[2]);

                double[] coords = {lat, lon, elev};
                
                arrListCoords.add(coords);
                }
                
            }
            
        }
        catch(Exception e)
        {
            System.out.println("Exception while reading file: " + e);                  
        }
        return arrListCoords;
         
        
    }
    

    
    public static void main(String[] args) {
        
        KMLParser k = new KMLParser();
        
        String fileName = "C:\\Users\\user\\Desktop\\pathCoords.kml";
         
        ArrayList arrListMyCoords = k.parseKML(fileName);
        
        for (double[] myCoords : arrListMyCoords){
            System.out.println(myCoords[0] + ", " + myCoords[1] + ", " + myCoords[2]);
         }
         
    }
    
}