Simple XML in Android 1.5 and Up

If you are looking for an easy way to use XML in Android then you have come to the right blog post. In this post I will take you all of the way through setting up the simple-xml library on Android so that you do not have to do so yourself and so that you can avoid the pitfalls that I did. Let us begin with a quick introduction and the requirements.

TL;DR: You can see the simple example project and run it by checking out this code example (BitBucket rocks): https://bitbucket.org/robertmassaioli/simple-xml-tutorial

About Simple and Android

I am assuming that when you have come to this blog post that you know what Android is, and that you have setup an Android Development Environment. If it just so happens that:

And then there is the Simple Framework for XML itself. Now you may not have heard of Simple before but it is a very handy annotation based package for dealing with XML that was written by Niall Gallagher and released under the LGPL Apache License (according to Niall himself in the comments below). Yes, you heard that correctly: LGPL Apache baby! For those of you that do not understand why that is great, the answer is this: you can legally use Simple in a commercial game. However, if you use the library, it might be nice you must give Niall Gallagher the proper attribution in the credits/acknowledgements section somewhere and send him a thanks for the great work; good developers love thanks (so perhaps you understand what I mean). This means that we can sell all and any of our Android apps that use the Simple framework. (Though that is not legal advice; IANAL)

Now, the question you might be having is: Why do we even want to use Android and Simple together anyway? I am glad you asked. The essential answer is that with the Simple XML library you will save heaps of developer time as your serialization and persistence code becomes an absolute breeze. You could struggle through with the provided Android libraries, or you could just use Simple and apply one annotation to a field and watch everything just magically work for you. Now the choice is yours, but I prefer the “one word, single annotation and everything just works” approach. If you do too then please read on and we’ll get started.

Download the library

Now it is time to Download Simple XML and find the appropriate JAR file to use in our project. So, lets go over to the Simple download page and download the latest version. (Though you should note that this tutorial uses Simple v2.5.2) Once that has been downloaded to your computer just unzip the package to somewhere. Inside the unzipped directory you will find a file that resided at jar/simple-xml-x.x.x.jar. This is the file that you want.

Example Android 1.5 Project

Important: In this example we use an Android 1.5 project but that means that simple should work with any Android project that is Cupcake (v1.5) or higher.

Here are the set of instructions that you should follow just to get the project skeleton created:

  1. Open Eclipse
  2. Press Ctrl+n, type in ‘Android’, select ‘Android Project’ and hit next to start creating an Android Project.
  3. In the new windows ‘Project Name’ Field put in ‘AndroidCupcakeSimpleTest’ as the project name. (Though this can be anything you like)
  4. In the ‘Build Target’ frame check the target called ‘Android 1.5′. This says that we are going to use the Android 1.5 API.
    If you do not have that option then you have not installed the Android 1.5 API and you should go do that from the ‘Android SDK and AVD’ manager.
  5. Put ‘Simple XML Test’ in the ‘Application Name’ text box.
  6. Put ‘com.simpletest.test’ in the ‘Package name’ text box.
  7. Put ‘ApplicationMain’ in the ‘Create Activity’ text box.
  8. Put ‘3’ in the ‘Min SDK Version’ text box.
  9. Click ‘Finish’ to actually create the project.

And that will give you an Android Cupcake project that you can use to test out the simple API, now to include the simple library in the project itself.

Including the External Simple Library Jar

Now we actually want to get the simple library into the Android project and that could not be easier.

  1. Make sure that you know where the simple-xml.jar file is.
  2. In Eclipse, Press Ctrl+n and make a folder in the base directory of the project called ‘libs’.
  3. Copy the simple-xml.jar file into that ‘libs’ directory.
  4. In Eclipse, click on the libs directory in the ‘Project Explorer’ and hit F5 to refresh the directory.
  5. Still in the project directory: right click on the simple-xml.jar file and, in the menu that appears, select ‘Build Path -> Add to Build Path’

Congratulations, the simple library is now included in your Android project and you can use it for great win and victory.

Advanced Android: Those of you that are more curious may know that Android can only read Dalvik bytecode; which is different to normal Java bytecode. Thus, you may be wondering, “How can the classes in the simple-xml jar file, which were compiled into different bytecode, now be made to work on a Dalvik VM? Shouldn’t we need to do something to those classes inside the jar first so that Dalvik can read them?” And that is an excellent question, with the answer being that you are completely correct; this will not ‘just work’ and something does need to do it for you. Luckily for you, this is magically done for you by the ADT plugin for Eclipse. The ADT plugin itself uses the ‘dx’ program that comes with the android SDK to compile all of the external jar classes into a single ‘classes.dex’ file that is again packaged in the final APK. You can read what is inside the ‘classes.dex’ file by extracting it from the APK and then running ‘dexdump’ on it (another program that comes with the Android SDK). Infact, if you want to see a pretty version of the dexdump then you can use an awesome program called ‘Dedexer’ written by Gabor Paller.

Writing some classes that use Simple XML

There is a comprehensive tutorial on the Simple website that teaches you how to use simple; so I am not really going to explain how this works. Look at the tutorial there to understand the details. I am just going to provide you with some code here to prove to you how easy it is to actually read this XML annotation style of code: thus convincing you that you really do want this for your own Android projects. So, without any further notes, here are the classes (please make sure that you put them in the correct packages or you will need to change the package paths):

Level.class

package com.simpletest.test.objects;

import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root
public class Level {
	@ElementList
	public List<GameObject> gameObjects = new ArrayList<GameObject>();
}

GameObject.class

package com.simpletest.test.objects;

public interface GameObject {
	int getObjectId();
}

Apple.class

package com.simpletest.test.objects;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;

public class Apple implements GameObject {
	private final int seedCount;
	private final int id;

	public Apple(@Attribute(name = "id") int id, @Element(name = "seedCount") int seedCount) {
		super();
		this.seedCount = seedCount;
		this.id = id;
	}

	@Element(name = "seedCount")
	public int getSeedCount() {
		return this.seedCount;
	}

	@Attribute(name = "id")
	@Override
	public int getObjectId() {
		return id;
	}
}

Orange.class

package com.simpletest.test.objects;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;

public class Orange implements GameObject {
	@Element
	public double containedJuice;
	private final int id;

	public Orange(@Attribute(name = "id") int id) {
		super();
		this.id = id;
	}

	@Attribute(name = "id")
	@Override
	public int getObjectId() {
		return id;
	}
}

WriteExample.class

package com.simpletest.test;

import java.io.File;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.util.Log;

import com.simpletest.test.objects.Apple;
import com.simpletest.test.objects.Level;
import com.simpletest.test.objects.Orange;

public class WriteExample {
	private final static String TAG = WriteExample.class.getCanonicalName();

	public WriteExample() {
		// Create the Dummy Level with Apples and Oranges
    	Log.i(TAG, "Create level");
    	Level level = new Level();
    	Orange orange;
    	level.gameObjects.add(new Apple(1, 10));
    	orange = new Orange(2);
    	orange.containedJuice = 20;
    	level.gameObjects.add(orange);
    	orange = new Orange(3);
    	orange.containedJuice = 100;
    	level.gameObjects.add(orange);
    	level.gameObjects.add(new Apple(4, 0));

    	// Now write the level out to a file
    	Log.i(TAG, "Write Level to file.");
    	Serializer serial = new Persister();
    	File sdcardFile = new File("/sdcard/levelout.xml");

    	try {
			serial.write(level, sdcardFile);
		} catch (Exception e) {
			// There is the possibility of error for a number of reasons. Handle this appropriately in your code
			e.printStackTrace();
		}
		Log.i(TAG, "XML Written to File: " + sdcardFile.getAbsolutePath());
	}
}

ApplicationMain.class

package com.simpletest.test;

import android.app.Activity;
import android.os.Bundle;

public class ApplicationMain extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	new WriteExample();
    	// new ReadExample(getResources()); // Uncomment this line in the next section.
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Hopefully, this all reads easily for you, but the basic gist of it is that you annotated a bunch of levels and game objects and then you serialised a level you created into XML form and out to the ‘/sdcard/levelout.xml’ file on your Android devices SD card. If you run the program in the Android emulator then (once the line “XML Written to File” appears in the LogCat output) you can get the file like this:

$ adb pull /sdcard/levelout.xml levelout.xml

And you can look at the output like this:

$ cat levelout.xml
<level>
   <gameObjects class="java.util.ArrayList">
      <gameObject class="com.simple.test.objects.Apple" id="1">
         <seedCount>10</seedCount>
      </gameObject>
      <gameObject class="com.simple.test.objects.Orange" id="2">
         <containedJuice>20.0</containedJuice>
      </gameObject>
      <gameObject class="com.simple.test.objects.Orange" id="3">
         <containedJuice>100.0</containedJuice>
      </gameObject>
      <gameObject class="com.simple.test.objects.Apple" id="4">
         <seedCount>0</seedCount>
      </gameObject>
   </gameObjects>
</level>
$

And just look at that output, it is beautiful! And it contains all of the information that you need to know for your class to be read back in. This is big. Now, with ease, you can dump any object tree in your Android program to XML and store it on disk, or send it to a server or even just print it on the screen. Everything that you can use XML for is now at your fingertips.

Advanced Android: When you ran the previous example, if you were being particularly observant and clever, then you would have been watching the LogCat output coming out of the emulator and seen some interesting/weird warnings in the logs right when the XML was being written out. The warnings that you would have seen would have looked something like this:

I/com.simpletest.test.ApplicationMain( 1587): Write Level to file.
E/dalvikvm( 1587): Could not find method javax.xml.stream.XMLInputFactory.newInstance, referenced from method org.simpleframework.xml.stream.StreamProvider.
W/dalvikvm( 1587): VFY: unable to resolve static method 262: Ljavax/xml/stream/XMLInputFactory;.newInstance ()Ljavax/xml/stream/XMLInputFactory;
W/dalvikvm( 1587): VFY:  rejecting opcode 0x71 at 0x0003
W/dalvikvm( 1587): VFY:  rejected Lorg/simpleframework/xml/stream/StreamProvider;. ()V
W/dalvikvm( 1587): Verifier rejected class Lorg/simpleframework/xml/stream/StreamProvider;
I/global  ( 1587): Default buffer size used in BufferedWriter constructor. It would be better to be explicit if an 8k-char buffer is required.

Now these are some odd errors so, please, allow me to explain what is going on here. The first line (with “Write Level to file.”) can be ignored, it is saying that the writing process is starting and that Simple XML is about to start doing its thing. The next five lines are all from Simple XML saying that there were some Verifier errors in the Simple XML framework; in a nutshell, right here is where Simple XML works some reflection magic to see what classes it can actually use to serialize your XML. Simple XML was made to work essentially anywhere (which is why it works on Android 1.5) and this reflection code tells it what its current platform gives it to work with. It also caches the result of this reflection so you will only see these error messages appear once and then Simple XML has automatically ‘reconfigured’ itself, if you like.

The final line with the global info warning is essentially stating that the BufferedWriter constructor that Simple XML uses allocates, for itself, 8KB of buffer memory and that might be more memory than you need. This is not an issue; just the Android Developers wishing that they could break the spec and force everyone else to use the other version of the BufferedWriter constructor. As it turns out however, 8 kilobytes is a pretty good starting buffer size for dealing with XML files. For anybody that is interested you can actually see the decision made by the Android team in the comments of the Android BufferedWriter code.

Reading XML into a Class

Now that we can write XML to a file can we do the opposite and read XML from a file in? Well naturally the answer is ‘yes’. Lets start by copying the XML that we got from the last example into a raw android resource xml file called “example.xml”. This file should exist at /res/raw/example.xml in the project hierarchy. Do that right now.

Then you should add another class that does the reading:

ReadExample.class

package com.simpletest.test;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.util.Log;

import com.simpletest.test.objects.Level;

public class ReadExample {
	private final static String TAG = ReadExample.class.getCanonicalName();

	public ReadExample(Resources resources) {
		Serializer serial = new Persister();

		try {
			Level levelRead = serial.read(Level.class, resources.openRawResource(R.raw.example));
			Log.i(TAG, "Objects in Level: " + levelRead.gameObjects.size());
		} catch (NotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Important: Don’t forget to go to the ApplicationMain class and uncomment the code that calls the ReaderExample.

As you can see, the code above will read the XML file that you put in your Android project and generate an entire hierarchy of objects for you to use. It allows you to read XML from a file in an incredibly easy and intuitive way.

Things to Watch Out For

However, I should say that all was not so easy when I first tried to read in XML from a raw resource. I was getting some weird errors where it was claiming that there was nothing, or barely anything, that it could read from my provided file. What happened was that the XML file that it was trying to read in was not being read with the correct encoding (UTF-8). Therefore it was seeing what it thought was a 0 character (EOF) on the second character read and just ending the parse there erroneously. If you get a similar problem then the solution is to get the raw input stream from the openRawResource(int) function, read that entire stream into a byte[], pass that byte[] into a String(byte[], String) constructor with the second argument being “UTF-8″ and then, and only then, pass that string into the Simple XML read(Class, String) function to be read. Doing that will ensure that the bytes from your input stream will be read with the correct character encoding and that you will be able to parse the file correctly.

That is the only caveat that I came across in my use of Simple XML on Android and it has a big Sourceforge page that I created looking into the issue.

Conclusion

In summary, this is how you can use an easy annotation based XML serialization framework on Android (that gives JAXB like functionality). It is very flexible and easy to use, and will make your projects easier to write. I know that this was a bit of a long post so thankyou for sticking with me; I hope that you get some good use out of this and that it helps those Android developers out there.

About these ads

43 thoughts on “Simple XML in Android 1.5 and Up

  1. Great post! I think this is also useful in passing objects around between different Activities in your app. Instead of writing to a file, you transform the object into an XML string, add it to the Bundle, and send it as part of the Intent. The receiving activity can then transform it back into an object.

    • Thankyou for the thanks, if you re-read the blog post it actually does explain how to read from an SD card: by simply opening a File object to a file located on that SD card. My example line would be:

          	File sdcardFile = new File("/sdcard/levelout.xml");
      

      This allows you to put an XML file into the Simple XML reader and get an object tree as a result. Just use that file in the read example and you will have working code.

  2. Brilliant!! you saved my life!
    took my a while to figure out how to use the osd result (i actually used your other tutorial.. this case it would be level), but once i did, its amazing.

    perhaps a little addition to the tutorial could be how to use the result, and see how powerful and easy simple is

    thanks a lot!

    cheers
    arjan

  3. Thanks for the blog post, it was very helpful.

    I have a suggestion for your read example. Rather than suggesting that people put the file into “/res/raw”, have them put it into “/assets”. Like /res/raw, /assets is a place you can place content that will be bundled with your application. However, anything in the /res directory is presented with resource ids and some amount of processing while /assets simply holds files that you access with an input stream. I had the same issue you did when I put my xml file into /res/raw, but no problem when I put it into /assets.

    A code snippet would look something like this:

    Serializer serial = new Persister();
    AssetManager assetManager = resources.getAssets();

    try {
    InputStream inputStream = assetManager.open(“example.xml”);
    Level levelRead = serial.read(Level.class, inputStream);
    }

  4. Thanks for the post, but I ran into some errors.
    I followed your steps and get the following errors:07-28 16:31:41.476: WARN/System.err(3258): org.simpleframework.xml.core.ElementException: Unable to determine generic type for field ‘gameObjects’ public java.util.List com.simpletest.test.objects.Level.gameObjects
    07-28 16:31:41.476: WARN/System.err(3258): at org.simpleframework.xml.core.ElementListLabel.getDependent(ElementListLabel.java:292)
    07-28 16:31:41.476: WARN/System.err(3258): at org.simpleframework.xml.core.CacheLabel.(CacheLabel.java:126)
    07-28 16:31:41.476: WARN/System.err(3258): at org.simpleframework.xml.core.LabelFactory.getInstance(LabelFactory.java:85)
    07-28 16:31:41.476: WARN/System.err(3258): at org.simpleframework.xml.core.LabelFactory.getInstance(LabelFactory.java:63)
    07-28 16:31:41.476: WARN/System.err(3258): at org.simpleframework.xml.core.StructureBuilder.process(StructureBuilder.java:236)
    07-28 16:31:41.476: WARN/System.err(3258): at org.simpleframework.xml.core.StructureBuilder.process(StructureBuilder.java:175)
    07-28 16:31:41.476: WARN/System.err(3258): at org.simpleframework.xml.core.Scanner.field(Scanner.java:465)
    07-28 16:31:41.476: WARN/System.err(3258): at org.simpleframework.xml.core.Scanner.scan(Scanner.java:350)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Scanner.(Scanner.java:91)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.ScannerFactory.getInstance(ScannerFactory.java:66)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Support.getScanner(Support.java:170)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Support.getName(Support.java:232)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Source.getName(Source.java:250)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Traverser.getName(Traverser.java:284)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Traverser.write(Traverser.java:203)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Traverser.write(Traverser.java:186)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Persister.write(Persister.java:1187)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Persister.write(Persister.java:1169)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Persister.write(Persister.java:1147)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Persister.write(Persister.java:1266)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Persister.write(Persister.java:1248)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Persister.write(Persister.java:1229)
    07-28 16:31:41.480: WARN/System.err(3258): at org.simpleframework.xml.core.Persister.write(Persister.java:1208)
    07-28 16:31:41.480: WARN/System.err(3258): at com.simpletest.test.WriteExample.(WriteExample.java:43)
    07-28 16:31:41.480: WARN/System.err(3258): at com.simpletest.test.ApplicationMain.onCreate(ApplicationMain.java:10)
    07-28 16:31:41.480: WARN/System.err(3258): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    07-28 16:31:41.480: WARN/System.err(3258): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    07-28 16:31:41.484: WARN/System.err(3258): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    07-28 16:31:41.484: WARN/System.err(3258): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    07-28 16:31:41.484: WARN/System.err(3258): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    07-28 16:31:41.484: WARN/System.err(3258): at android.os.Handler.dispatchMessage(Handler.java:99)
    07-28 16:31:41.484: WARN/System.err(3258): at android.os.Looper.loop(Looper.java:130)
    07-28 16:31:41.484: WARN/System.err(3258): at android.app.ActivityThread.main(ActivityThread.java:3683)
    07-28 16:31:41.484: WARN/System.err(3258): at java.lang.reflect.Method.invokeNative(Native Method)
    07-28 16:31:41.484: WARN/System.err(3258): at java.lang.reflect.Method.invoke(Method.java:507)
    07-28 16:31:41.484: WARN/System.err(3258): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    07-28 16:31:41.492: WARN/System.err(3258): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    07-28 16:31:41.492: WARN/System.err(3258): at dalvik.system.NativeStart.main(Native Method)

  5. SimpleXml depends on stax, which uses the packages structure: javax.xml…
    (see for example: http://mvnrepository.com/artifact/org.simpleframework/simple-xml/2.6)

    So you will get errors when trying to create the distribution file (apk file), since Android doesn’t allow you to use anything in the javax. packages.

    (Eclipse will hide those errors from you but they are still there, and you can see them at the command line if you do : ant release).

  6. I ran in the same errors/warnings as above mentioned. The example is not working for me. The File is created but it stays empty.

    Anyway, for some reason I also had to change the path names, please consider to use Environment.getExternalStorageDirectory () to get the right directory. Some devices have another data structure.

    Best regards,
    Rotesmofa

  7. I am concerned about performance, using something like XmlPullParser Vs the Simple XML library. Anybody has done any performance measurements?

    If I am repeatedly processing many XMLs of the same type, does the system remembers/cache previously collected information about the Java class mappings or does it use reflection every time to figure out how to map XML elements to classes?

    • In terms of performance measurements no, I have not done any. When I made this blog post the intention was easy of programming over speed. However, I load and save XML files on Android devices quite regularly and I can say that speed is never an issue for me.

      What I would recommend, since Simple XML is so easy to write, is to just write it using Simple XML first and then, if it is too slow for your needs, rewrite it in the much more annoying XmlPullParser.

  8. I’ve been looking around for some information/example of how Simple XML handles Generics in Java. Do you by any chance know how this is done programmatically? Jackson and Gson solve it with the TypeReference and TypeToken classes, respectively (for JSON), but I haven’t found anything similar in Simple XML.

    Thanks.
    -Vito

    • I have not looked at it closely but I think you should take a closer look at the ClassInstance.java file in the Simple XML codebase and also at the class=”package.blah” attributes that come out of using Simple XML to serialise a generic class or interface. Sorry that I cannot really be of more help; all I really know is that it works like I expect it to.

  9. The Simple seems to be promising. Can you tell me how to set multiple attributes, e.g. I need to set both inline=true and required=false for an ElementList.
    Thanks a lot,
    Rex

  10. Pingback: Simple XML | Andrew Nash

  11. Hi, How did you get around with dalvik error complaining about javax.* classes? for me it failed with
    error message.

    Dx
    trouble processing “javax/xml/stream/events/StartElement.class”:

    Ill-advised or mistaken usage of a core class (java.* or javax.*)
    when not building a core library.

  12. My XML String contains & elements. While using method serializer.read(AppointmentsResponse.class, xmlStr), I am getting error ::

    Exception in thread “main” org.simpleframework.xml.core.ElementException: Element ‘Body’ does not have a match in class pojo.AppointmentsResponse at line 3

    Any clue how to deal with such xml files ?

    • I usually do not like performing Q & A on this blog but I do like answering questions in general therefore, if you wish to get a response to your question, please go post this question on Stack Overflow and then reply to this message with the link to the question you made. When you make the question make sure you include the definition of AppointmentsResponse. If you do that then I will happily look over it for you. And the best part is that, if I cannot solve it, there might be somebody else on Stack Overflow that can.

  13. Hi Mr. Massaioli,
    I like the tutorial however it assumes I know how to set up a class in Java properly to receive the XML data.
    I’m trying to port a class from XNA that imports XML for spritesheet animation. In XNA, the class is pretty straightforward as XNA simplifies the process. As I’m new to java/android programming, I don’t know how to properly set up my classes to receive the data.

    I’ve posted a question on Stack Overflow, if you could look at it and maybe give me a few tips I would appreciate it: http://stackoverflow.com/questions/13427862/setting-up-class-for-xml-deserialization-using-simple-xna-to-java-porting

    Regards,
    Hashim

  14. Good explanation! I want to acknowledge you about a little mistake which caused exception. You forgot to write “@Root” before defining Apple and Orange class.

    Cheers :)

    • That is strange that it caused an exception, unless you are directly writing out the Apple or Orange class as the root node it should not cause an exception. But thanks for catching that.

  15. HI. I follow the tutoriel seriously and i fall in that error :
    09-06 13:15:08.940: W/System.err(1622): java.lang.ClassNotFoundException: Didn’t find class “com.simple.test.objects.Apple” on path: DexPathList[[zip file "/data/app/com.simpletest.test-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.simpletest.test-2, /system/lib]]

    I can’t read or write. Why ?
    thanks

    • I have mentioned all of the information about “licensing stuff” that you need to know to get started above (in this very blog post). Just search the page for license and you should be able to find it pretty quickly.

  16. Hi. I am using Android 4.2.2 and dalvik is complaining the below. The program still runs fine. my question is if there is a way to resolved the complains or can I just ignore them?

    11-20 09:14:19.748: I/dalvikvm(816): Could not find method javax.xml.stream.XMLInputFactory.newInstance, referenced from method org.simpleframework.xml.stream.StreamProvider.
    11-20 09:14:19.748: W/dalvikvm(816): VFY: unable to resolve static method 5827: Ljavax/xml/stream/XMLInputFactory;.newInstance ()Ljavax/xml/stream/XMLInputFactory;
    11-20 09:14:19.748: D/dalvikvm(816): VFY: replacing opcode 0x71 at 0x0003
    11-20 09:14:19.748: W/dalvikvm(816): VFY: unable to find class referenced in signature (Ljavax/xml/stream/XMLEventReader;)
    11-20 09:14:19.748: W/dalvikvm(816): VFY: unable to find class referenced in signature (Ljavax/xml/stream/XMLEventReader;)
    11-20 09:14:19.748: I/dalvikvm(816): Could not find method javax.xml.stream.XMLInputFactory.createXMLEventReader, referenced from method org.simpleframework.xml.stream.StreamProvider.provide
    11-20 09:14:19.748: W/dalvikvm(816): VFY: unable to resolve virtual method 5825: Ljavax/xml/stream/XMLInputFactory;.createXMLEventReader (Ljava/io/InputStream;)Ljavax/xml/stream/XMLEventReader;
    11-20 09:14:19.748: D/dalvikvm(816): VFY: replacing opcode 0x6e at 0x0002
    11-20 09:14:19.748: I/dalvikvm(816): Could not find method javax.xml.stream.XMLInputFactory.createXMLEventReader, referenced from method org.simpleframework.xml.stream.StreamProvider.provide
    11-20 09:14:19.748: W/dalvikvm(816): VFY: unable to resolve virtual method 5826: Ljavax/xml/stream/XMLInputFactory;.createXMLEventReader (Ljava/io/Reader;)Ljavax/xml/stream/XMLEventReader;

  17. Hi,
    I tried to use SimpleXml on android. Every things works well, except when I use a Converter. Its works in Java, but not in Android : the converter is never called and the corresponding element not deserialized.
    Is there something to do for Converter to work ?

    • I made a small test with a converter and it works. So my problem must be in my code, not in using a converter
      Sorry for disturbing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s