Вы могли бы просто вырастить свой собственный маленький парсер для этой конкретной задачи, однако использование парсера XML было бы действительно хорошей идеей:
Класс узлов (простой парсер узлов) включены в класс):
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Nodes {
private String node_ID;
private double latitude;
private double longitude;
private String name;
private String waypoint_Type;
private int level_ID;
private String on_Starting_Route;
private String on_Finish_Route;
// Constructor #1
public Nodes() { }
// Constructor #2
public Nodes(String nodeID, double latitude, double longitude, String name,
String waypointType, int levelID, String startingRoute,
String finishRoute) {
this.node_ID = nodeID;
this.latitude = latitude;
this.longitude = longitude;
this.name = name;
this.waypoint_Type = waypointType;
this.level_ID = levelID;
this.on_Starting_Route = startingRoute;
this.on_Finish_Route = finishRoute;
}
// Getters and Setters
public String getNode_ID() {
return node_ID;
}
public void setNode_ID(String node_ID) {
this.node_ID = node_ID;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWaypoint_Type() {
return waypoint_Type;
}
public void setWaypoint_Type(String waypoint_Type) {
this.waypoint_Type = waypoint_Type;
}
public int getLevel_ID() {
return level_ID;
}
public void setLevel_ID(int level_ID) {
this.level_ID = level_ID;
}
public String getOn_Starting_Route() {
return on_Starting_Route;
}
public void setOn_Starting_Route(String on_Starting_Route) {
this.on_Starting_Route = on_Starting_Route;
}
public String getOn_Finish_Route() {
return on_Finish_Route;
}
public void setOn_Finish_Route(String on_Finish_Route) {
this.on_Finish_Route = on_Finish_Route;
}
// Read in Nodes from XML (simple parser)
public List<Nodes> getNodesFromFile(String filePath) {
List<Nodes> nodes = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.equals("")) {
continue;
}
if (line.toLowerCase().startsWith("<node id=\"")) {
String nodeID;
double lat = 0.0d;
double lon = 0.0d;
String name = "";
String wayType = "";
int levID = 0;
String startR = "";
String finishR = "";
nodeID = line.substring("<node id=\"".length(), line.lastIndexOf("\">"));
int count = 0;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.equalsIgnoreCase("</node>")) {
break;
}
if (line.equals("")) {
continue;
}
count++;
switch (count) {
case 1:
String la = line.substring((line.contains("<data key=\"lat\">")
? "<data key=\"lat\">".length() : "<data key=\"lat\"".length()),
(line.contains("</data>") ? line.indexOf("</data>") : line.indexOf("/>")));
lat = (la.matches("-?\\d+(\\.\\d+)?") ? Double.parseDouble(la) : 0.0d);
break;
case 2:
String lg = line.substring((line.contains("<data key=\"lon\">")
? "<data key=\"lon\">".length() : "<data key=\"lon\"".length()),
(line.contains("</data>") ? line.indexOf("</data>") : line.indexOf("/>")));
lon = (lg.matches("-?\\d+(\\.\\d+)?") ? Double.parseDouble(lg) : 0.0d);
break;
case 3:
String nm = line.substring((line.contains("<data key=\"name\">")
? "<data key=\"name\">".length() : "<data key=\"name\"".length()),
(line.contains("</data>") ? line.indexOf("</data>") : line.indexOf("/>")));
name = nm.trim().equals("") ? "" : nm;
break;
case 4:
String wt = line.substring((line.contains("<data key=\"waypoint_type\">")
? "<data key=\"waypoint_type\">".length() : "<data key=\"waypoint_type\"".length()),
(line.contains("</data>") ? line.indexOf("</data>") : line.indexOf("/>")));
wayType = wt.trim().equals("") ? "" : wt;
break;
case 5:
String lid = line.substring((line.contains("<data key=\"level_id\">")
? "<data key=\"level_id\">".length() : "<data key=\"level_id\"".length()),
(line.contains("</data>") ? line.indexOf("</data>") : line.indexOf("/>")));
levID = (lid.matches("^-?\\d+$") ? Integer.parseInt(lid) : 0);
break;
case 6:
String sr = line.substring((line.contains("<data key=\"on_starting_route\">")
? "<data key=\"on_starting_route\">".length() : "<data key=\"on_starting_route\"".length()),
(line.contains("</data>") ? line.indexOf("</data>") : line.indexOf("/>")));
startR = sr.trim().equals("") ? "" : sr;
break;
case 7:
String fr = line.substring((line.contains("<data key=\"on_finish_route\">")
? "<data key=\"on_finish_route\">".length() : "<data key=\"on_finish_route\"".length()),
(line.contains("</data>") ? line.indexOf("</data>") : line.indexOf("/>")));
finishR = fr.trim().equals("") ? "" : fr;
break;
}
}
nodes.add(new Nodes(nodeID, lat, lon, name, wayType, levID, startR, finishR));
}
}
}
catch (FileNotFoundException ex) {
System.err.println(ex.getMessage());
}
catch (IOException ex) {
System.err.println(ex.getMessage());
}
return nodes;
}
// The toString() method
@Override
public String toString() {
return new StringBuffer("").append("Node ID = ").append(node_ID).append(", Latitude = ")
.append(latitude).append(", Longitude = ").append(longitude).append(", Name = ")
.append(name).append(", Waypoint Type = ").append(waypoint_Type).append(", Level ID = ")
.append(level_ID).append(", On Starting Route = ").append(on_Starting_Route)
.append(", On Finish Route = ").append(on_Finish_Route).toString();
}
}
Для использования класса узлов:
Вот как вы можете использовать это класс:
Nodes n = new Nodes();
List<Nodes> nodes = n.getNodesFromFile("Graphml.xml");
// Display the Nodes in Console Window...
if (!nodes.isEmpty()) {
for (Nodes node : nodes) {
// System.out.println(node.toString());
System.out.printf("%-20s %-15s%n", "Node ID:", node.getNode_ID());
System.out.printf("%-20s %-15s%n", "Latitude:", node.getLatitude());
System.out.printf("%-20s %-15s%n", "Longitude:", node.getLongitude());
System.out.printf("%-20s %-15s%n", "Name:", node.getName());
System.out.printf("%-20s %-15s%n", "Waypoint Type:", node.getWaypoint_Type());
System.out.printf("%-20s %-15s%n", "Level ID:", node.getLevel_ID());
System.out.printf("%-20s %-15s%n", "On Starting Route:", node.getOn_Starting_Route());
System.out.printf("%-20s %-15s%n", "On Finish Route:", node.getOn_Finish_Route());
System.out.println();
}
}