如何在每个元素的末尾创建一个解析器组列表?

时间:2014-07-26 05:16:52

标签: java xml parsing size

我创建了一个方法,用于解析包含位置坐标的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),而我打算将它作为LineStrings的数量(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 个答案:

答案 0 :(得分:0)

只需要进行微小的改动:

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);
}