简体   繁体   中英

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

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

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.

就我而言,在删除 xml 文件中的前导 3 个字节 (BOM=0xEF BB BF) 后,错误消失了。

I had same error, I just removed appended string from my doInBackground() method! it was :

@Override
protected String doInBackground(String... strings) {
  Log.d(TAG, "doInBackground: starts with: " + strings[0]);
  String rssFeed = downloadXML(strings[0]);
  return "test:\n "+rssFeed;
}

and now:

@Override
protected String doInBackground(String... strings) {
  Log.d(TAG, "doInBackground: starts with: " + strings[0]);
  String rssFeed = downloadXML(strings[0]);
  return rssFeed;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM