Android - 帮我解决NodeList问题

时间:2012-05-15 15:00:11

标签: java android xml-parsing

我这里有问题。 我想测试我的解析器结果。

我有一个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)。你可以帮我解决我的问题吗?

谢谢,我非常抱歉我的英语&gt; _&lt;

1 个答案:

答案 0 :(得分: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");
        }