Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlPullParser - unexpected token (android)

i am developing an app, that will read from xml that i have storied in res/xml/experiment.xml, but when i try to parse it, it gives me an xmlPullParserException.

Here is my really simple xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <message>Hello</message>

Here is my code:

    public static void parse(Context ctx) throws XmlPullParserException, IOException {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    XmlPullParser xpp = factory.newPullParser();

    InputStream in = ctx.getResources().openRawResource(R.xml.experiment);

    xpp.setInput(in, "UTF_8");

    int eventType = xpp.getEventType();

    while (eventType != XmlPullParser.END_DOCUMENT) {
        String tagName = xpp.getName();

        switch (eventType) {
        case XmlPullParser.START_TAG:
            Log.d("debug", "Entering tag: " + tagName);

            break;
        case XmlPullParser.TEXT:
            Log.d("debug", "Text inside: " + xpp.getText());

            break;
        case XmlPullParser.END_TAG:
            Log.d("debug", "Ending tag: " + tagName);

            break;

        }

        eventType = xpp.next();
    }

}

And here is the exception it throws me:

    03-16 15:38:52.759: W/System.err(28087): org.xmlpull.v1.XmlPullParserException:       Unexpected token (position:TEXT ???????????????8??????...@2:112 in      java.io.InputStreamReader@42604888) 
    03-16 15:38:52.759: W/System.err(28087):    at org.kxml2.io.KXmlParser.next(KXmlParser.java:426)
    03-16 15:38:52.759: W/System.err(28087):    at    org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
    03-16 15:38:52.759: W/System.err(28087):    at xmlparsing.Xmlreader.parse(Xmlreader.java:53)
    03-16 15:38:52.759: W/System.err(28087):    at com.example.androidexperiments.Lifecycle.onCreate(Lifecycle.java:28)
    03-16 15:38:52.759: W/System.err(28087):    at android.app.Activity.performCreate(Activity.java:5231)
    03-16 15:38:52.759: W/System.err(28087):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    03-16 15:38:52.759: W/System.err(28087):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    03-16 15:38:52.759: W/System.err(28087):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    03-16 15:38:52.759: W/System.err(28087):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    03-16 15:38:52.759: W/System.err(28087):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    03-16 15:38:52.759: W/System.err(28087):    at android.os.Handler.dispatchMessage(Handler.java:102)
    03-16 15:38:52.759: W/System.err(28087):    at android.os.Looper.loop(Looper.java:136)
    03-16 15:38:52.759: W/System.err(28087):    at android.app.ActivityThread.main(ActivityThread.java:5017)
    03-16 15:38:52.759: W/System.err(28087):    at java.lang.reflect.Method.invokeNative(Native Method)
    03-16 15:38:52.759: W/System.err(28087):    at java.lang.reflect.Method.invoke(Method.java:515)
    03-16 15:38:52.759: W/System.err(28087):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    03-16 15:38:52.759: W/System.err(28087):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    03-16 15:38:52.759: W/System.err(28087):    at dalvik.system.NativeStart.main(Native Method)

I spend few hours on google trying to solve it, but i still have no idea, can somebody point me where is the problem?

Thanks

like image 474
David Novák Avatar asked Mar 16 '14 15:03

David Novák


3 Answers

not sure if you're still stuck on this. I had at least a similar problem. I wouldn't say I've found the solution, but I've found a workaround that helped in my situation anyway. The reason I think we have the same problem is because of what it says in the exception .. @2:112. As you probably know that means row 2, column 112 of the input. Considering the brevity of your xml input, this is obviously ridiculous as your 2nd line of input doesn't have anywhere close to 112 columns. I was seeing a similarly bogus position in my exception from my simple input.

I think the problem might be the way you're acquiring the InputStream:

InputStream in = ctx.getResources().openRawResource(R.xml.experiment);

If you take the time to convert the input in the InputStream to a String (code for this can be found), you will see that each line returns bogus data; there's a lot of junk characters mixed in with xml data characters. I don't know why. Maybe someone more versed in Java can answer that.. I suspect it's because the xml file is being opened as a raw resource (just like it says) and it needs to be opened with some kind of encoding,, like utf-8 to translate it correctly.. ? Anyway, when I used the .openRawResource(..) function, it seemed to come with a lot of junk data in the InputStream, which choked the XmlPullParser. My solution to this was to move the .xml file from the res/xml/ folder to the assets folder. I then acquired the InputStream in this manner.

InputStream in = this.getAssets().open("sample.xml");

When I did that, I noticed there were no junk characters in the InputStream, and XmlPullParser was able to parse my file without any exceptions.

Hope that helps, good luck.

like image 154
yano Avatar answered Sep 29 '22 23:09

yano


I think the error might be coming from the encoding you've set to your XmlPullParser.

This line is incorrect: xpp.setInput(in, "UTF_8");. It should be: xpp.setInput(in, "UTF-8");. If it doesn't help, you can even try changing that two lines by:

InputStream in = ctx.getResources().openRawResource(R.xml.experiment);
xpp.setInput(in, null);
like image 37
nKn Avatar answered Sep 29 '22 21:09

nKn


In my case, the error disappeared after I removed the leading 3 bytes (BOM=0xEF BB BF) in xml-file.

like image 33
RVV Avatar answered Sep 29 '22 22:09

RVV