XmlPullParser - unexpected token (android)

David Novák picture David Novák · Mar 16, 2014 · Viewed 12.2k times · Source

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

Answer

nKn picture nKn · Mar 16, 2014

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);