我正在尝试解析包含地理节点的 XML 以及使用 SAX 解析器连接节点的方式。我将解析的节点存储在 an 中ArrayMap<Long, MapPos>
,将方式存储在ArrayList<ArrayList<MapPos>>
. 解析方式时,我创建一个ArrayList<MapPos>
引用节点并将其添加到ArrayList
方式中。
调试应用程序后,我看到startElement()
并endElement()
成功将方法添加到.ArrayList
endDocument()
ArrayList
ArrayList
这是java类:
public class ParkingDataExtractor {
private static List<ArrayList<MapPos>> roads = new ArrayList<ArrayList<MapPos>>();
public static List<ArrayList<MapPos>> getWaysFromXML()
throws ParserConfigurationException, SAXException, IOException{
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
DefaultHandler handler = new DefaultHandler() {
ArrayMap<Long, MapPos> nodes = new ArrayMap<Long, MapPos>();
ArrayList<MapPos> nodeBuffer = new ArrayList<MapPos>();
List<ArrayList<MapPos>> ways = new ArrayList<ArrayList<MapPos>>();
// private int i; // for debug purposes
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("node")) {
Long id = Long.parseLong(attributes.getValue("id"));
Float lat = Float
.parseFloat(attributes.getValue("lat"));
Float lon = Float
.parseFloat(attributes.getValue("lon"));
nodes.put(id, new MapPos(lat, lon));
} else if (qName.equalsIgnoreCase("nd")) {
Long ref = Long.parseLong(attributes.getValue("ref"));
nodeBuffer.add(nodes.get(ref));
}
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
if (qName.equalsIgnoreCase("way")) {
ways.add(nodeBuffer);
// i++;
// if(i==1590) // last element
// ArrayList<MapPos> test = ways.get(i-1); // test = [MapPos [x=..., y=..., z=0.0], MapPos [x=..., y=..., z=0.0],...]
nodeBuffer.clear();
}
}
@Override
public void endDocument() throws SAXException {
// ArrayList<MapPos> test = ways.get(i-1); // test = []
roads = ways;
}
};
saxParser.parse("file://" + Environment.getExternalStorageDirectory()
+ "/roadmap.xml", handler);
return roads;
}
}