Version of SimpleXML: 2.5.2
Platform: Android Cupcake (v1.5)
Code:
Serializer serializer = new Persister();
try {
Level test = serializer.read(Level.class, getResources().openRawResource(R.xml.testlevel));
} catch (NotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
=== Begin Level.java ===
package com.massaiolir.mol.levels;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;
@Root(name = "level")
public class Level {
@Attribute(name = "number")
private int levelNumber;
}
=== End Level.java ===
=== Begin testlevel.xml ===
<?xml version="1.0" encoding="UTF-8"?>
<level number="0">
</level>
=== End testlever.xml ===
StackTrace:
E/AndroidRuntime( 3823): java.lang.StackOverflowError
E/AndroidRuntime( 3823): at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:557)
E/AndroidRuntime( 3823): at org.kxml2.io.KXmlParser.nextImpl(KXmlParser.java:305)
E/AndroidRuntime( 3823): at org.kxml2.io.KXmlParser.next(KXmlParser.java:1330)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:104)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:115)
E/AndroidRuntime( 3823): at org.simpleframework.xml.stream.PullRead
Okay so I spent some time looking into the problem and I have located a section of code that it is in. In the file PullReader.read() the event variable coming out of parser.next is 1. Now looking at the docs (http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html#END_DOCUMENT) a 1 represents the end of the document. Therefore, since this function does not expect the end of the document it is recursing infinitely until it stack overflows. I do not know why it does not expect the end of the document but something needs to be done to take this case into account. Please let me know if this helps.
So I got bored on the bus on the way in to work and looked up this bug on my iPhone. I think the solution us as simple as if(event == End_document) return null;
(sent from my iPhone)
Actually I do not think that the problem is due to SimpleXML anymore. If you take a look at: getResources().openRawResource(R.xml.testlevel) it does not return what you would expect and as a result it is failing... weird. Looking into that a little more.
So as it turns out that Android is giving back the file in UTF-8 format and when you called the setInput(source, null) function it was not being told that the format was, in-fact, UTF-8. And it did not manage to recognise the file correctly. Therefore when it saw that the second byte in the input was 0 (which you would expect in a 16bit format) it was treating it as the (char)0 and thinking that it was the end of the file. Thus it was ignoring basically all of the file. The fix is to replace null in the setInput function with "UTF-8" which is not a big deal. I will attach the patch that makes it work for me.
This patch fixes the bug for me.
Now the bit that weirds me out is that the way that you originally had it should just work even according to the Android docs: http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html#setInput\(java.io.InputStream, java.lang.String)
This makes me suspect that the Android docs are currently incorrect and that in v1.5 of the Android code it did not properly detect the type of data that it was getting in the setInput function.
Therefore, if you do not want to directly apply my patch then may I humbly suggest the creation of a read(Class, InputStream, String) function where the extra string represents the file format that it should try and parse as? And make that the base function and make the original read(Class, InputStream) function delegate to that by passing a 'null' in the String argument so no functionality is lost.
I really look forward to hearing back to you about this, I don't actually know if you are getting any of these messages.
P.S. Thanks for the awesome package btw. I decided to put my actions where my mouth was, in this one, and solve the problem myself so that you do not have to. Cheers.
For all of you Android developers out there that want a temporary workaround so that you can get Simple XML working in Android well all you have to do is read the raw file into a byte array and then use that byte array in the String(byte[], String) function with the string argument being the encoding format of the XML file that you are loading (In my case "UTF-8"). And then you can pass that string into the Simple XML read function like normal. Too easy.
Wrote a blog post to better explain how to make an Android project that uses Simple: http://massaioli.homelinux.com/wordpress/2011/04/21/simple-xml-in-android-1-5-and-up/
This may not even be an error, I think perhaps this could be an XML document without a prolog, or perhaps an XML document with a prolog that contains the incorrect encoding. I will take a look. Could you provide the source document that you used to get this issue.
It might be good to either upload the document or add it in base64 so that the encoding does not change when you paste it on to the site.
What is the event that is returned each time the method recurses? Its obviously not START_TAG, END_TAG, or TEXT. If you could provide the event type that would be really useful.
This has been fixed in version 2.5.3.
Fixed stack overflow error.