2012-05-15 38 views
3

我有一個問題在這裏。 我想測試我的解析器結果。Android - 幫我NodeList

我有從請求到谷歌地圖的kml文件。這裏是我的KML文件

<LineString> 
    <coordinates> 
     106.826200,-6.297500,0.000000 
     106.826220,-6.297260,0.000000 
     106.826380,-6.297050,0.000000 
     106.826900,-6.296710,0.000000 
     106.827120,-6.296640,0.000000 
     106.827120,-6.296640,0.000000 
     106.827170,-6.296510,0.000000 
     106.827140,-6.296370,0.000000 
     106.827140,-6.296370,0.000000 
     106.826210,-6.295840,0.000000 
     106.824970,-6.295220,0.000000 
     106.823550,-6.294580,0.000000 
     106.822690,-6.293830,0.000000 
     106.822860,-6.293800,0.000000 
     106.823820,-6.294160,0.000000 
     106.825240,-6.294830,0.000000 
     106.830400,-6.297550,0.000000 
     106.831360,-6.298100,0.000000 
     106.885600,-6.293860,0.000000 
    </coordinates> 
</LineString> 

,這是我的代碼:

NodeList nl = doc.getElementsByTagName("LineString"); 
     for(int s = 0; s < nl.getLength(); s++){ 
      Node rootNode = nl.item(s); 
      NodeList configItems = rootNode.getChildNodes(); 
      for(int x = 0; x < configItems.getLength(); x++){ 
       Node lineStringNode = configItems.item(x); 
       NodeList path = lineStringNode.getChildNodes(); 
       pathConent = path.item(0).getNodeValue(); 
      } 
     } 

解析器是成功地,我可以在谷歌地圖上繪製路線。但現在我想知道它的工作原理,所以我想打印TextView的座標。這裏是我的新代碼:

NodeList nl = doc.getElementsByTagName("LineString"); 
     for(int s = 0; s < nl.getLength(); s++){ 
      Node rootNode = nl.item(s); 
      NodeList configItems = rootNode.getChildNodes(); 
      for(int x = 0; x < configItems.getLength(); x++){ 
       Node lineStringNode = configItems.item(x); 
       NodeList path = lineStringNode.getChildNodes(); 
       pathConent = path.item(0).getNodeValue(); 
      } 
     } 
    String[] tempContent = pathConent.split(" "); 
      for (int i = 0; i < tempContent.length; i++){ 
       koor.setText("Latitude, Longitude:\n" + tempContent[i] + "\n"); 
      } 

但爲什麼在我的TextView我只得到了第一個座標系下(106.826200,-6.297500,0.000000)。你能幫我解決我的問題嗎?

感謝,我真的對我的英語> _不好意思使用,而不是<

+0

嘗試節點列表NL = doc.getElementsByTagName( 「座標」);而不是NodeList nl = doc.getElementsByTagName(「LineString」);你也可以在那裏放置一個斷點,看看變量是否獲得了你期望的值。 – Asdfg

+0

從我可以看到你應該得到最後的coords,因爲你在for循環的每次迭代中爲'TextView'寫了一個新值。你應該使用'StringBuilder'來構建你想要顯示的文本,然後使用一個'TextView.setText'指令。 – techiServices

回答

2

String[] tempContent = pathConent.split(" "); 
    koor.setText(""); 
    for (int i = 0; i < tempContent.length; i++){ 
     koor.append("Latitude, Longitude:\n" + tempContent[i] + "\n"); 
    } 

String[] tempContent = pathConent.split(" "); 
     for (int i = 0; i < tempContent.length; i++){ 
      koor.setText("Latitude, Longitude:\n" + tempContent[i] + "\n"); 
     } 
+0

非常感謝你的幫助。很高興知道你 – DevYudh

+1

隨時歡迎friend.happy編碼!!!!! –