XmlPullParser - 意外的标记(机器人)机器人、标记、意外、XmlPullParser

2023-09-07 22:34:11 作者:倾尽繁华只为你

我开发一个应用程序,这将从XML,我已经层林尽在RES / XML / experiment.xml读,但是当我尝试分析它,它给了我一个xmlPullParserException。

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.

下面是我的真正简单的XML文件:

Here is my really simple xml file:

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

下面是我的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?

感谢

推荐答案

不知道,如果你还停留在此。我至少有一个类似的问题。我不会说我已经找到的在的解决方案,但我已经找到了解决办法在我的处境,帮助反正。我认为,我们有同样的问题的原因是因为它说,在异常.. @ 2:112。正如你可能知道,这意味着第2行中,输入112列。考虑到你的XML输入的简洁,这显然是荒谬的输入您的第二行没有任何接近112列。我看到从我简单的输入在我的异常类似伪造的位置。

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.

我认为这个问题可能是你获取的InputStream的方式:

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

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

如果您需要到输入的的InputStream转换为字符串(code此可以找到)的时候,你会看到,每一行返回虚假数据;有很多的混合与XML数据字符乱码。我不知道为什么。也许有人更精通Java可以回答这个问题。我怀疑这是因为XML文件被打开作为原始资源(就像它说的),并需要将它打开某种编码,,比如UTF-8翻译它正确..?无论如何,当我使用了 .openRawResource(..)函数,它似乎配备了大量的垃圾数据的InputStream的,它窒息了XmlPullParser。我的解决方案,这是从 RES / XML / 文件夹中的.xml文件移动到资产文件夹。然后,我收购了的InputStream以这种方式。

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

当我这样做,我注意到有在InputStream中没有垃圾字符,XmlPullParser能够解析我的文件,没有任何例外。

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.

希望帮助,祝你好运。