7

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

2
  • Which is the line 53 of your Xmlreader.java file? Commented Mar 16, 2014 at 15:08
  • This one eventType = xpp.next(); Commented Mar 16, 2014 at 15:28

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Just came across this problem myself. Thanks for finally getting it.
5

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

2 Comments

That doesn't help either. It still throws me the same error
That looks like answer to me too.
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.