如何从java生成动态kml

时间:2013-10-17 11:09:22

标签: java xml kml

我是KML新手我必须制作一个KML文件,其中包含有关某个地点的一些信息,并且该数据应该显示在Google地图中。我在java中编写了一个代码,它会生成一个KML作为输出,但是我有一些问题,KML没有生成。 java.io.FileNotFoundException:c:\ PlaceMarkers.kml(访问被拒绝)这是我的错误.. 这就是我到目前为止所做的......

    import java.io.*;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.Result;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;

   public class GenKMLPlaceMarker {

public int id;
public String name;
public String address;
public float lat;
public float lng;
public String type;

public static void main(String[] args) {

    Statement stmt;
    ResultSet rs;
    GenKMLPlaceMarker KML = new GenKMLPlaceMarker();

    try {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/homeland";
        Connection con = DriverManager.getConnection(url, "root", "root");
        DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
        Document doc = builder.newDocument();
        Element root = doc.createElement("kml");
        root.setAttribute("xmlns", "http://earth.google.com/kml/2.1");
        doc.appendChild(root);
        Element dnode = doc.createElement("Document");
        root.appendChild(dnode);
        Element rstyle = doc.createElement("Style");
        rstyle.setAttribute("id", "restaurantStyle");
        Element ristyle = doc.createElement("IconStyle");
        ristyle.setAttribute("id", "restaurantIcon");
        Element ricon = doc.createElement("Icon");
        Element riconhref = doc.createElement("href");
        riconhref
                .appendChild(doc
                        .createTextNode("http://maps.google.com/mapfiles/kml/pal2/icon63.png"));
        rstyle.appendChild(ristyle);
        ricon.appendChild(riconhref);
        ristyle.appendChild(ricon);
        dnode.appendChild(rstyle);
        Element bstyle = doc.createElement("Style");
        bstyle.setAttribute("id", "barStyle");
        Element bistyle = doc.createElement("IconStyle");
        bistyle.setAttribute("id", "barIcon");
        Element bicon = doc.createElement("Icon");
        Element biconhref = doc.createElement("href");
        biconhref
                .appendChild(doc
                        .createTextNode("http://maps.google.com/mapfiles/kml/pal2/icon27.png"));
        bstyle.appendChild(bistyle);
        bicon.appendChild(biconhref);
        bistyle.appendChild(bicon);
        dnode.appendChild(bstyle);
        stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT * FROM markers");
        while (rs.next()) {
            KML.id = rs.getInt("id");
            KML.name = rs.getString("name");
            KML.address = rs.getString("address");
            KML.lat = rs.getFloat("lat");
            KML.lng = rs.getFloat("lng");
            KML.type = rs.getString("type");
            Element placemark = doc.createElement("Placemark");
            dnode.appendChild(placemark);
            Element name = doc.createElement("name");
            name.appendChild(doc.createTextNode(KML.name));
            placemark.appendChild(name);
            Element descrip = doc.createElement("description");
            descrip.appendChild(doc.createTextNode(KML.address));
            placemark.appendChild(descrip);
            Element styleUrl = doc.createElement("styleUrl");
            styleUrl.appendChild(doc.createTextNode("#" + KML.type
                    + "Style"));
            placemark.appendChild(styleUrl);
            Element point = doc.createElement("Point");
            Element coordinates = doc.createElement("coordinates");
            coordinates.appendChild(doc.createTextNode(KML.lng + ","
                    + KML.lat));
            point.appendChild(coordinates);
            placemark.appendChild(point);
        }
        Source src = new DOMSource(doc);
        Result dest = new StreamResult(new File("c:/PlaceMarkers.kml"));
        aTransformer.transform(src, dest);
        System.out.println("Completed.....");
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

}

1 个答案:

答案 0 :(得分:4)

您可以使用Java API for KML
Java API for KML的目标是提供Java接口,以便轻松访问KML(Keyhole标记语言)数据。

Java API for KML(JAK)的主要目标是提供由OGC的KML标准和Google的GX扩展定义的KML对象模型的自动生成的完整参考实现。它是面向对象的API,可以在现有Java环境中方便易用地使用KML。