Skip to content

(Android) Parsing xml file which contains locations in Zoo

illa edited this page Feb 28, 2014 · 3 revisions

Parsing xml file

To parse xml file a method parse(Context ctx, File file) from ParserHelper class should be used - it opens, parses, closes xml file and returns parsed data in ZooLocationsData object.

Developer can choose file which will be parsed but it will work only in debug mode. If application is not in debug mode, parser will use predefined file from resources.

Example:

Context ctx = ...
File file = ...
ParserHelper ph = new ParserHelper();
try {
	ZooLocationsData data = ph.parse(ctx, file);
} catch (IOException e) {
 ...
} catch (XmlPullParserException e) {
 ...
}

If developer wants to use a file from resources, argument file should be null.

Using ZooLocationsData

Each Node, Animal, Way, Junction and ZooLocationsData has standard methods set and get.

Example - write out all animals, ways and junctions in data:

ZooLocationsData data;
...
for (Animal a : data.getAnimals()) {
	Log.i(LOG_TAG, a.getName() + ", latitude: " + a.getNode().getLatitude() + ", longitude: " + a.getNode().getLongitude() );
}
for (Way w : data.getWays()) {
	Log.i(LOG_TAG, "way: " + w.getId());
	for (Node n : w.getNodes()) {
		Log.i(LOG_TAG, "latitude: " + n.getLatitude() + ", longitude: " + n.getLongitude() );
	 }
}
for (Junction j : data.getJunctions()) {
	Log.i(LOG_TAG, "junction - latitude: " + j.getNode().getLatitude() + " , longitude: " + j.getNode().getLongitude());
	for (Way w : j.getWays()) {
		Log.i(LOG_TAG, "way: " + w.getId());
	 }
}

###For QA

Currently:

  • in debug mode: file .myguide/data2.xml (on external storage) is always opened. If any error occures, application opens res/raw/data.xml.

  • in release build: file res/raw/data.xml is always opened.