【发布时间】:2012-05-15 15:00:11
【问题描述】:
我这里有问题。 我想测试我的解析器结果。
我有一份来自 Google 地图的请求的 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)。你能帮我解决我的问题吗?
谢谢,我真的很抱歉我的英语>_
【问题讨论】:
-
尝试 NodeList nl = doc.getElementsByTagName("coordinates");而不是 NodeList nl = doc.getElementsByTagName("LineString");你也可以在里面放一个断点,看看变量是否得到了你期望的值。
-
据我所见,您应该只获得最后一个坐标,因为您在 for 循环的每次迭代中都向
TextView写入了一个新值。您应该使用StringBuilder来构建要显示的文本,然后使用单个TextView.setText指令。
标签: java android xml-parsing