package readkmlfile;
import com.ximpleware.AutoPilot;
import com.ximpleware.NavException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException;
/**
 * @author Marco Berri marcoberri@gmail.com 
 * @see http://tecnicume.blogspot.com
 */
public class ReadKmlFile {
    /**
     * @param args the command line arguments
     * @throws XPathEvalException
     * @throws NavException  
     */
    public static void main(String[] args) throws XPathEvalException, NavException {
        VTDGen vg = new VTDGen();
        vg.parseFile("/Users/marco/Desktop/test.kml", true);
        System.out.println("Start");
        AutoPilot ap;
        VTDNav vn = vg.getNav();
        try {
            vn.toElement(VTDNav.ROOT);
            vn.matchElement("kml");
            ap = new AutoPilot(vn);
            ap.selectXPath("/kml/Placemark/*");
        } catch (XPathParseException ex) {
            System.out.println(ex.getMessage());
            return;
        } catch (NavException ex) {
            System.out.println(ex.getMessage());
            return;
        }
        int r = 0;
        while ((r = ap.evalXPath()) != -1) {
            System.out.println(vn.toString(r));
            if (vn.toString(r).equals("LineString")) {
                VTDNav vnls = vn.cloneNav();
                if (vnls.toElement(VTDNav.FC)) {
                    do {
                        if (vnls.toString(vnls.getCurrentIndex()).equals("coordinates")) {
                            String coordinates = vnls.toString(vnls.getText());
                            String[] lines = coordinates.split("\\n");
                            for (String l : lines) {
                                if (l.length() == 0) {
                                    continue;
                                }
                                String[] block = l.split(",");
                                System.out.println("lat:" + block[0].trim());
                                System.out.println("lon:" + block[1].trim());
                                System.out.println("ele:" + block[2].trim());
                            }
                        }
                    } while (vnls.toElement(VTDNav.NEXT_SIBLING));
                }
            }
        }
    }
}
venerdì 27 gennaio 2012
Java - How to parse KML files
Parse KML files in Java with ximpleware lib:
Etichette:
Gps,
java,
Kml,
Ximpleware
Ubicazione:
13048 Santhià VC, Italia
Iscriviti a:
Commenti sul post (Atom)
 
Nessun commento:
Posta un commento