2009-07-16 92 views

回答

7

您將製作自己的圖書館,但您不會編寫任何代碼。

我建議看看http://code.google.com/apis/kml/documentation/kmlreference.html。從那裏你可以得到XML Schema。獲得架構後,您可以使用JAXB生成對象樹,以輕鬆解析和編寫KML。

This may also be a good resource,貌似別人已經做到了!

+1

我沿着這條路走了一段小小的練習,出現了一點點呃逆,就是在架構通過編譯器運行時碰撞了一些元素。沒有什麼重大的,但準備寫幾個自定義綁定。 – CurtainDog 2011-03-16 03:37:59

1

這裏是我的JSOUP實現希望它有助於

public ArrayList<ArrayList<LatLng>> getCoordinateArrays() { 
    ArrayList<ArrayList<LatLng>> allTracks = new ArrayList<ArrayList<LatLng>>(); 

    try { 
     StringBuilder buf = new StringBuilder(); 
     InputStream json = MyApplication.getInstance().getAssets().open("track.kml"); 
     BufferedReader in = new BufferedReader(new InputStreamReader(json)); 
     String str; 
         String buffer; 
     while ((str = in.readLine()) != null) { 
      buf.append(str); 
     } 

     in.close(); 
     String html = buf.toString(); 
     Document doc = Jsoup.parse(html, "", Parser.xmlParser()); 
     ArrayList<String> tracksString = new ArrayList<String>(); 
     for (Element e : doc.select("coordinates")) { 
      tracksString.add(e.toString().replace("<coordinates>", "").replace("</coordinates>", "")); 
     } 

     for (int i = 0; i < tracksString.size(); i++) { 
      ArrayList<LatLng> oneTrack = new ArrayList<LatLng>(); 
      ArrayList<String> oneTrackString = new ArrayList<String>(Arrays.asList(tracksString.get(i).split("\\s+"))); 
      for (int k = 1; k < oneTrackString.size(); k++) { 
       LatLng latLng = new LatLng(Double.parseDouble(oneTrackString.get(k).split(",")[0]), 
         Double.parseDouble(oneTrackString.get(k).split(",")[1])); 
       oneTrack.add(latLng); 
      } 
      allTracks.add(oneTrack); 
     }} 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return allTracks; 
} 
0

,如果你使用Android工作室:)

dependencies { 
    compile 'org.jsoup:jsoup:1.8.1' 
} 


     // find a way to read the file and store it in a string 

     String inputFileContents = ""; 
     String xmlContent = inputFileContents; 
     Document doc = Jsoup.parse(xml, "", Parser.xmlParser()); 

     for(Element e : doc.select("LineString").select("coordinates")) { 
      // the contents 
      System.out.println(e.text()); 
     } 

你可以有多個選擇()方法調用。 我簡化了代碼:

Element e = doc.select("LineString").select("coordinates").first(); 
+0

錯誤:(20,29)java:找不到解析合適的方法(org.jsoup.nodes.Document.OutputSettings.Syntax,java.lang.String,org.jsoup.parser.Parser) 方法org.jsoup.Jsoup .parse(java.lang.String,java.lang.String,org.jsoup.parser.Parser)不適用 (參數不匹配; org.jsoup.nodes.Document.OutputSettings.Syntax不能轉換爲java.lang。字符串) 方法 – user3871754 2017-04-10 07:53:25

+0

我的導入:import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.parser.Parser; import static org.jsoup.nodes.Document.OutputSettings.Syntax.xml; – user3871754 2017-04-10 07:53:58

1

這是其他選項,KML文件是一個正常的文件,誰包含結構的XML文件。 這是另一個例子,搜索一個特定的地標文件中的多個地標

private static void readKML(InputStream fileKML, String nameCoordinates) { 
    String column = null; 
    Boolean folder = Boolean.TRUE; 
    Boolean placemark = Boolean.FALSE; 
    Boolean placeCorrect = Boolean.FALSE; 
    BufferedReader br = new BufferedReader(new InputStreamReader(fileKML)); 
    try { 
     while ((column = br.readLine()) != null) { 
      if (folder) { 
       int ifolder = column.indexOf("<Folder>"); 
       if (ifolder != -1) { 
        folder = Boolean.FALSE; 
        placemark = Boolean.TRUE; 
        continue; 
       } 
      } 
      if (placemark) { 
       String tmpLine = nameCoordinates; 
       tmpLine = tmpLine.replaceAll("\t", ""); 
       tmpLine = tmpLine.replaceAll(" ", ""); 
       String tmpColumn = column; 
       tmpColumn = tmpColumn.replaceAll("\t", ""); 
       tmpColumn = tmpColumn.replaceAll(" ", ""); 
       int name = tmpColumn.indexOf(tmpLine); 
       if (name != -1) { 
        placemark = Boolean.FALSE; 
        placeCorrect = Boolean.TRUE; 
        continue; 
       } 
      } 
      if (placeCorrect) { 
       int coordin = column.indexOf("<coordinates>"); 
       if (coordin != -1) { 
        String tmpCoordin = column; 
        tmpCoordin = tmpCoordin.replaceAll(" ", ""); 
        tmpCoordin = tmpCoordin.replaceAll("\t", ""); 
        tmpCoordin = tmpCoordin.replaceAll("<coordinates>", ""); 
        tmpCoordin = tmpCoordin 
          .replaceAll("</coordinates>", ""); 
        String[] coo = tmpCoordin.split(","); 
        System.out.println("LONG: "+coo[0]); 
        System.out.println("LATI: "+coo[1]) 
        break; 
       } 
      } 

     } 
     br.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return cresp; 
}