从Android上的资源解析XML文件 [英] Parsing XML File from res on Android

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

问题描述

我试图分析我有一个名为原料本身包含我的Andr​​oid应用程序的res文件夹中的文件夹中定义的XML文件。我已经从本质上如何处理XML文件中的Andr​​oid开发者指南中使用的版本。

I am trying to parse an XML file that I have defined inside a folder named raw which itself is contained inside the res folder of my android application. I have essentially used the version from the Android Developers guide on how to process XML files.

http://developer.android.com/training/basics/网络-OPS / xml.html

目前,应用程序将运行并找到该文件,然后运行解析方式,但都应该找到要提取的数据标签的方法似乎跳过,因此我喜欢的类型单位的数组列表中有在这种情况下没有名字的单位。

Currently the app will run and will locate the file, then run the parse method but the methods that are supposed to locate the tags that you want to extract data from seem to skip and as a result my array list of type Unit has in this instance a Unit with no name.

下面是解析XML文件,然后我使用来测试XML文件中的类的副本。

Below is a copy of the class that parses the XML file followed by the XML file I am using to test.

public class UnitTableXMLParser extends AsyncTask<InputStream, Void, Void> {
    private final String ns = null;

    public ArrayList<Unit> parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readFeed(parser);
        } finally {
            in.close();
        }
    }

    private ArrayList<Unit> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, ns, "resources");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();

            if (name.equals("unit")) {
                units.add(readUnit(parser));
            } else {
                skip(parser);
            }
        }
        return units;
    }

    private Unit readUnit(XmlPullParser parser) throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, ns, "unit");
        String name = null;
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }

            String parserName = parser.getName();

            if (parserName.equals("name")) {
                name = readName(parser);
            }
        }
        return new Unit(name);
    }

    private String readName(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "name");
        String name = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "name");
        return name;
    }

    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }

    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                depth--;
                break;
            case XmlPullParser.START_TAG:
                depth++;
                break;
            }
        }
    }

    @Override
    protected Void doInBackground(InputStream... params) {
        Log.d(TAG, "UnitTableXMLParser exectued");
        try {
            parse(params[0]);
        } catch (IOException e) {
            Log.d("Background Thread IOException", e.getMessage());
        } catch (XmlPullParserException e) {
            Log.d("Background Thread XmlPullParserException", e.getMessage());
        } catch (Exception e) {
            Log.d("Exception", e.getMessage());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        xmlParsed = true;
        super.onPostExecute(result);
    }
}

和XML文件:

和输出:

And the output :

任何想法,我可能会错误将是非常美联社preciated,我已经试过name.matches并包含字符串匹配然而这也不能工作。

Any ideas where I might be going wrong would be much appreciated, I have tried name.matches and contains for String matching yet that didn't work either.

干杯,

杰米

推荐答案

只需通过nextTag()规范的样子。接下来,它调用(),如果它是START_TAG或以其他方式END_TAG抛出一个新的XMLPullParserException返回的事件。如果它符合文本或文档块没有complanations它是如何工作的。
 很明显,是这种情况。所以,你的所有试戴块breaked和结果为空。什么可怕的例子,可怕的文档给developer.android.com!
 我不能给你的工作答案 - 我是新来的Andr​​oid ...

Just look through the nextTag() specification. It calls next() and return event if it is START_TAG or END_TAG otherwise throw a new XMLPullParserException. And no complanations how does it work if it meets text or doc-blocks. It's obvious that is the case. So, all your try- block is breaked and result is null. What an awful example and awful documentation gave developer.android.com! And I couldn't give you working answer- i'm new to Android...

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

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