0

我正在尝试解析我在名为 raw 的文件夹中定义的 XML 文件,该文件夹本身包含在我的 android 应用程序的 res 文件夹中。我基本上使用了 Android 开发者指南中关于如何处理 XML 文件的版本。

http://developer.android.com/training/basics/network-ops/xml.html

目前,应用程序将运行并定位文件,然后运行 ​​parse 方法,但应该定位您要从中提取数据的标签的方法似乎跳过,因此我的 Unit 类型数组列表在此实例中具有一个没有名字的单位。

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

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 文件:

XML 文件

和输出: 输出

任何我可能出错的想法都将不胜感激,我已经尝试过 name.matches 和 contains 用于字符串匹配,但也没有用。

干杯,

杰米

4

1 回答 1

0

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...

于 2012-10-03T06:09:30.713 回答