XML pull解析器资产XML [英] xml pull parser assets xml

查看:173
本文介绍了XML pull解析器资产XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析的资产中的本地XML文件夹使用pull解析器?我不能让拉解析器可以工作。它总是抛出IO异常。我想,我不能让文件路径,或连接到该文件。

How can i parse a local XML file in the assets folder using pull parser? I can't get pull parser to work. It always throws an io exception. I think I can't get the path to the file, or connecting to the file.

推荐答案

mixm,

我是问各种方式从两个'资产'和'水库'加载本地文件,但回答你的问题被玩弄(因为没有其他人似乎有):

I was toying with various ways to load a local file from both 'assets' and 'res', but to answer your question as asked (as no one else seems to have):

首先,可以确保你的XML是在测试前有效或关闭验证,这是你如何能做到这一点,实例化一个pull解析器在同一时间:

First, either make sure your XML is valid before testing or turn off validation, this is how you can do that and instantiate a pull parser at the same time:

    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setValidating(false);
        XmlPullParser myxml = factory.newPullParser();

然后打开XML文件,并设置为输入到你的拉解析器:

Then open the xml file and set as input to your pull parser:

        InputStream raw = getApplicationContext().getAssets().open("simple.xml");
        myxml.setInput(raw, null);

现在设置您的拉环(或其他,取决于你是否要推迟做拉或不,这是你的设计决策:

Now setup your pull loop (or other, depends on whether you want to do deferred pulling or not, that's your design decisions:

        int eventType = myxml.getEventType();
        while(eventType != XmlPullParser.END_DOCUMENT) {
            if(eventType == XmlPullParser.START_DOCUMENT) {

                Log.d(MY_DEBUG_TAG, "In start document");
            }
            else if(eventType == XmlPullParser.START_TAG) {
                Log.d(MY_DEBUG_TAG, "In start tag = "+myxml.getName());
            }
            else if(eventType == XmlPullParser.END_TAG) {
                Log.d(MY_DEBUG_TAG, "In end tag = "+myxml.getName());

            }
            else if(eventType == XmlPullParser.TEXT) {
                Log.d(MY_DEBUG_TAG, "Have text = "+myxml.getText());
            }
            eventType = myxml.next();
        }
    } catch (XmlPullParserException e) {

注意 myxml.getEventType(),你需要做到这一点得到了解析会,处理什么类型的事件,你是拉。注:省略了可读性catch块

Note the myxml.getEventType() , you need to do this to get the parse going and handle what type events you are pulling. Note: catch blocks omitted for readability.

测试上方2.1,希望它有助于 -Frank

Tested the above on 2.1, hope it helps -Frank

这篇关于XML pull解析器资产XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆