taking XML file values to put to hashmap



I am trying to parse "way" in the XML file and reading the values into a HashMap.. My code lists out the ways but it does not stop, it lists all the points. I tried to put break; to hopefully stop the process but then the program does not print anything, can someone help me? Thanks in advance.


I am using Java / Eclipse.


private void extractWays(File file) { ArrayList wayPoints = new ArrayList();



try {
scanner = new Scanner(file);
while(scanner.hasNext()) {
String line = scanner.nextLine().replaceAll("^\\s+", "");
while (!line.startsWith("</way>")) {
if (line.startsWith("<way")) {
this.id = Long.parseLong(OSM.extractStringFromVal(line, "id"));

}
if (line.startsWith("<nd")) {
this.ref = Long.parseLong(OSM.extractStringFromVal(line, "ref"));

} else if (line.startsWith("</way>")) {
wayPoints.add(ref);
ways.put(id, wayPoints);
}
break;
}
}
}
catch (Exception ex) {
ex.printStackTrace();
return;
}
for (Long key : ways.keySet()) {
System.out.println(key + " => " + ways.get(key));
}
}

No comments:

Post a Comment