Android Saving and Loading XML Data

Android persistence can be user initied, automatically executed at intervals or more importantly activity initiated if the activity quits when in the background so that the player can resume from exactly where they left off next time they load the app.

There are different ways to do this but this post is about using XML serialization, more specifically on using the Simple XML framework which is fully compatible with Android. I'm a big fan of it as it's strong proponent for convention over configuration.

Lets start with a game app example; say the user starts a new game, the activity will need to read the game data from somewhere initially. Put the 'template' or default game data xml file in ./assets/props folder in the Android project (create the /props folder inside assets if its not there already). Creating a FileUtilites class for read/write generic functions might be useful - in the sample below you can see the loadProperties method which returns a reader. The context object is from the activity which is used to return the assets and the filename that is passed to this methods is "props/gamedata.xml", for example. 

	public static Reader loadProperties(String filename, Context context) {
		Resources resources = context.getResources();
		try {
			InputStream is = context.getAssets().open(filename);
			int size = is.available();
			// Read the entire asset into a local byte buffer.
			byte[] buffer = new byte[size];
			is.read(buffer);
			is.close();
			// Convert the buffer into a string.
			String text = new String(buffer);
			Reader reader = new StringReader(text);
			return reader;
		} catch (IOException e) {
			// Should never happen!
			throw new RuntimeException(e);
		}
	}

The loadProperties functions returns a reader for the XML file, and this is where Simple XML comes into play. We create a new serializer object (to use it just include the necessary Simple XML classes in your class), which can then convert the XML from the reader to a Java object that can be your game data. Keep in mind, the idea is that the data in your properties folder will never be changed by your application, it is there to initialise, your app will persist the actual modifiable data to a different place. 

	Serializer serializer = new Persister();
	try {
		reader = FileUtils.loadProperties("props/"+ mapPropertiesFilename, context);
		tree = serializer.read(RPGCards.class, reader, false);
		return tree;
	} catch (Exception e) {
		// Should never happen!
		throw new RuntimeException(e);
	}

The next step is to persist the application data to the data folder, i'm tempted to say cache but it's not that, think of it as your app specific storage that your app can write to. The code below is pretty much self explanatory. The save location is "/data/data/com.packagename/" ,the package name is your app package. 

//write data 
	Serializer serializer = new Persister();
	File result = new File("/data/data/com.packagename/", "gamesave.xml");
	try {
		serializer.write(map, result);
	} catch (Exception e1) {
		e1.printStackTrace();
	}

and to load it from the data folder, in a similar manner

//read data
	File file = new File("/data/data/com.packagename/", "gamesave.xml");
	if (file.exists() ) { 
		Log.d(TAG, "load propertieslocation for player saveg game data ");
		try {
			map = serializer.read(Map.class, file);
			return map;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

You can get Simple XML here http://simple.sourceforge.net/ - highly recommended!