有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在每个元素的末尾列出解析器组?

我创建了一个方法,用于解析包含位置坐标的XML数据。XML数据的格式如下所示:

// Coordinates are of the form '-180,90,0 -120,60,0 -90,45,0 ...'
<LineString>
 <coordinates>
   coordinate1 coordinate2 coordinate3...
 </coordinates>
</LineString>
<LineString>
 <coordinates>
   coordinate11 coordinate12 coordinate13...
 </coordinates>
</LineString>
<LineString>
 <coordinates>
   coordinate29 coordinate30 coordinate31... //(# of coordinates is different per list)
 </coordinates>
</LineString>
... etc

它正确地解析了XML文件,但我似乎犯了一个错误,即存储坐标的列表的大小是坐标的总数(292),而我希望它是行字符串的数量(9)

这是我创建的用于分析数据的方法:

if(qName.equals("coordinates")) {
 String[] splitspace = accumulator.toString().trim().split(" ");
 Segment listcoords = new Segment();

 for (String s : splitspace) {
   strarray = s.split(",");
   Double strarray1 = Double.parseDouble(strarray[0]);
   Double strarray2 = Double.parseDouble(strarray[1]);
   Coordinates coord = new Coordinates(strarray1, strarray2);
   listcoords.addPoint(coord); //addPoint takes two coordinates to create an object Coordinates
   route.addSegment(listcoords); //addSegment takes lists of coordinates and adds them to a Segment which is a list of Coordinates.
}

有人能帮我确定如何将段的大小从292更改为9吗


共 (1) 个答案

  1. # 1 楼答案

    只需要一个小的改动:

    if(qName.equals("coordinates")) {
        String[] splitspace = accumulator.toString().trim().split(" ");
        Segment listcoords = new Segment();
    
        for (String s : splitspace) {
            String[] strarray = s.split(",");
            Double strarray1 = Double.parseDouble(strarray[0]);
            Double strarray2 = Double.parseDouble(strarray[1]);
            Coordinates coord = new Coordinates(strarray1, strarray2);
            listcoords.addPoint(coord); 
        }
        // now the Segment list listcoords is complete and can be added to the global list of list
        route.addSegment(listcoords);
    }